I have a notifications table in my database which contains the attributes: timestamp:datetime
and read:boolean
. What I want is to query and order my notifications; first by if they are read or not, then after which timestamp they've got, then limit the amount of notifications to 10.
The query I've tried looks something like this:
@user.notifications.order_by([[:read,:desc],[:timestamp,:desc]]).limit(10)
Which only gives me an error which I narrowed down to proving the boolean field as the culprit.
Is there an existing way of ordering by true/false values or should I resort to using some kind of Custom Field Serialization transforming True's and False's to 1's and 0's?
What you specify works for me with Mongoid 2.4.10, mongo 1.3, rails 3.2.3. Hope that the following helps to address your problem.
test/unit/notification_test.rb
test output