Mongoid order_by 布尔值
我的数据库中有一个通知表,其中包含属性:timestamp:datetime
和 read:boolean
。我想要的是查询和订购我的通知;首先是它们是否被读取,然后是它们获得的时间戳,然后将通知数量限制为 10。
我尝试过的查询看起来像这样:
@user.notifications.order_by([[:read,:desc],[:timestamp,:desc]]).limit(10)
这只会给我一个错误,我将其范围缩小到证明布尔字段是罪魁祸首。
是否存在按真/假值排序的现有方法,或者我应该诉诸使用某种自定义字段序列化将 True 和 False 转换为 1 和 0?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您指定的内容适用于 Mongoid 2.4.10、mongo 1.3、rails 3.2.3。希望以下内容能够帮助解决您的问题。
test/unit/notification_test.rb
测试输出
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