0

这个链接很棒,但它没有解决在or条件下组合两个活动关系后获得 active_relation 的问题。还有其他链接,但
它们都没有回答上述问题。尽管 for anding 方法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

虽然这样做不是问题,但我宁愿将结果置于活动关系中,以便我可以执行任何进一步的活动记录查询。

任何想法......或更好地解决我的问题?

修改:-

我正在查看个人资料,发现这个问题没有任何活动,甚至没有评论,即使它可能被证明是至关重要的问题。我这么说是因为使用合并方法的第二个链接中显示的操作非常简单,但是不存在这样方法,或者为什么会这样???rails社区的专业人士不应该提供这种方法吗??? AR

4

1 回答 1

0

后来我找到了一种在or条件下组合两个活动关系的方法,但不是我想要的答案——有一种方法可以使用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))") 
于 2014-11-30T19:07:58.473 回答