我有两个型号
class Conversation
include Mongoid::Document
field :last_moderated_at, type: DateTime
has_many :messages
end
class Message
include Mongoid::Document
include Mongoid::Timestamps
end
我想获取在审核日期之后创建的所有消息的列表,或者如果审核日期为零则获取所有消息
我预计以下内容会起作用
conversation.messages.where(
:created_at.gte => conversation.last_moderated_at
)
但显然与 nil 的比较失败(当
last_moderated_at == nil
时)
我别无选择,只能使用
if/else
或者是否有一个 greater than
类型的 mongoDB 运算符与零日期相比也有效?
编辑:一个更简单的例子:
Conversation.where(:created_at.gte => nil).count
将始终返回0
语法正确。至少它可以在 Mongoid 版本 6 上运行。
我认为你必须这样做:
conversation = Conversation.find 'some_id'
Message.where(
conversation: conversation,
:created_at.gte => conversation.last_moderated_at
)