如何在OR条件下将两个ActiveRecord::Relation组合起来形成活动关系



这个链接很好,但它没有解决在条件下组合两个活动关系后获得active_relation的问题。还有其他链接,但
他们都没有回答上述问题。虽然对于and的方法merge起作用并给出了积极的联系,但我在寻找到or两个条件下的困境。为了阐明我的问题,假设我必须两个不同编写的查询,而不是单独执行并添加它们,这样就不会输出活动关系。例如:-

@incoming = Message.incoming_from_my_friends_only(@current_user) # where 'incoming_from_my_friends_only' is a active record query  
@outgoing = Message.outgoing_to_my_friends_only(@current_user) # same here

现在,我不想做这个

@incoming = Message.incoming_from_my_friends_only(@current_user) 
@outgoing = Message.outgoing_to_my_friends_only(@current_user)
@messages = @incoming + @outgoing

虽然这样做不是问题,但我更希望结果处于活动关系中,这样我就可以执行任何进一步的活动记录查询。

任何想法。。。还是更好地解决我的问题??

修订:-

我查看了个人资料,发现这个问题没有任何活动,甚至没有评论,尽管它可能被证明是至关重要的问题。我这么说是因为在AR上执行非常容易,如第二个链接所示,使用merge方法,但对于不存在这样的方法,为什么???rails社区的pro不应该提供这种方法吗???

我后来找到了一种在条件下组合两个活动关系的方法,但不是我想要的答案-有一种方法可以使用merge在活动关系上执行。我找到的答案是这样的。

# replace the comment part in where by the condition written for incoming_from_my_friends_only and outgoing_to_my_friends_only respectively
@incoming = Message.where("(#incoming_from_my_friends_only(@current_user)) or (outgoing_to_my_friends_only(@current_user))") 

最新更新