这个链接很好,但它没有解决在或条件下组合两个活动关系后获得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))")