将两个的ActiveRecord ::关联对象 [英] Combine two ActiveRecord::Relation objects
本文介绍了将两个的ActiveRecord ::关联对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下两个对象:
Suppose I have the following two objects:
first_name_relation = User.where(:first_name => 'Tobias') # ActiveRecord::Relation
last_name_relation = User.where(:last_name => 'Fünke') # ActiveRecord::Relation
是有可能的两个关系结合起来,产生含有两个条件之一的ActiveRecord ::关联
对象?
注:我知道,我可以链何在得到这个行为,就是我真正感兴趣的是,我有两个独立的的ActiveRecord ::关系<的情况下/ code>的对象。
的
推荐答案
使用<一个href="http://api.rubyonrails.org/classes/ActiveRecord/SpawnMethods.html#method-i-merge"><$c$c>merge$c$c>:
name_relation = first_name_relation.merge(last_name_relation)
注意合并
联合,其中
使用条款和
。尽管ActiveRecord的目前还没有使用结合关系的能力或
相反,它是未来的ActiveRecord的5(见的相关承诺)。
Note that merge
combines WHERE
clauses using AND
. While ActiveRecord does not currently have the ability to combine relations using OR
instead, it is coming in ActiveRecord 5 (see the relevant commit).
这篇关于将两个的ActiveRecord ::关联对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文