我有这两段代码,每段代码都返回 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
有没有办法做到这一点?