我有两段代码,每条代码都在Micropost模型中返回一个关系。
scope :including_replies, lambda { |user| where("microposts.in_reply_to = ?", user.id)}
def self.from_users_followed_by(user)
followed_user_ids = user.followed_user_ids
where("user_id IN (?) OR user_id = ?", followed_user_ids, user)
end
当我运行r1 = Micropost.including_replies(user)
时,我使用以下SQL得到两个结果的关系:
SELECT `microposts`.* FROM `microposts` WHERE (microposts.in_reply_to = 102) ORDER BY
microposts.created_at DESC
当我运行r2 = Micropost.from_users_followed_by(user)
时,我通过以下SQL获得与一个结果的关系:
SELECT `microposts`.* FROM `microposts` WHERE (user_id IN (NULL) OR user_id = 102) ORDER
BY microposts.created_at DESC
现在当我合并像r3 = r1.merge(r2)
这样的关系时,我的结果为零,但期待三次。原因是SQL看起来像这样:
SELECT `microposts`.* FROM `microposts` WHERE (microposts.in_reply_to = 102) AND
(user_id IN (NULL) OR user_id = 102) ORDER BY microposts.created_at DESC
现在我需要的是(microposts.in_reply_to = 102) OR (user_id IN (NULL) OR user_id = 102)
我需要在合并关系中使用OR
而不是AND
。
有办法做到这一点吗?
答案 0 :(得分:1)