似乎无法解决这个问题。我在下面有一个消息模型
Message
# content:string
# original_id:integer
# sender_id:integer
# receiver_id:integer
has_one :reply, class_name: "Message", foreign_key: "original_id"
belongs_to :original, class_name: "Message"
每条消息只能有一个回复,回复消息会有对应的原始消息。
我想做的是创建一个范围或类方法,允许我在一批中提取已回复的消息,并在另一批中提取未回复的消息。
就像是
# return messages that have a reply present
def self.replied
where(reply.present?)
end
# return messages that have no reply
def self.unreplied
where(reply.nil?)
end
所以我可以链接方法并最终拉出消息
user1.messages.replied
它目前不起作用,因为我不能使用 where 子句,除非它是一个 DB 列......所以我正在考虑在 DB 中添加一个“replied”布尔列,这样我就可以使用 where 子句但可能有一个解决方案对此,我只是没有考虑。带有 lambda 的作用域?我现在被困住了。
非常感谢任何帮助