我有两个模型
class Conversation
include Mongoid::Document
field :last_moderated_at, type: DateTime
has_many :messages
end
class Message
include Mongoid::Document
include Mongoid::Timestamps
end
我想获取在审核日期之后创建的所有消息的列表,或者如果审核日期为 nil 则获取所有消息
我曾期望以下工作
conversation.messages.where(
:created_at.gte => conversation.last_moderated_at
)
但显然与 nil 的比较失败了(当last_moderated_at == nil
)
我是否别无选择,只能使用或者与 nil 日期相比也可以使用if/else
类型的 mongoDB 运算符?greater than
编辑:一个更简单的例子:Conversation.where(:created_at.gte => nil).count
总是返回 0