Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在或条件下组合两个ActiveRecord::Relation以形成活动关系?_Ruby On Rails_Activerecord_Active Relation - Fatal编程技术网

Ruby on rails 如何在或条件下组合两个ActiveRecord::Relation以形成活动关系?

Ruby on rails 如何在或条件下组合两个ActiveRecord::Relation以形成活动关系?,ruby-on-rails,activerecord,active-relation,Ruby On Rails,Activerecord,Active Relation,此链接是,但它没有解决在或条件下组合两个活动关系后获得活动关系的问题。还有其他的,但是 他们都没有回答上述问题。虽然对于安定法的合并起到了积极的作用,但我的困境在于找到了一个或两个条件。为了阐明我的问题,假设我必须编写一个或两个不同的查询,而不是单独执行并添加它们,这样就不会输出活动关系。 例如: @incoming = Message.incoming_from_my_friends_only(@current_user) # where 'incoming_from_my_friends_o

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

@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上执行和是非常容易的,如使用合并方法的第二个链接所示,但没有这样的方法存在,或者为什么会这样???rails社区的pro不应该提供这种方法吗

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

# 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))")