将两个的ActiveRecord ::关联对象对象、两个、ActiveRecord

由网友(逆光、盛夏)分享简介:假设我有以下两个对象:Suppose I have the following two objects:first_name_relation = User.where(:first_name => 'Tobias') # ActiveRecord::Relationlast_name_relation = U...

假设我有以下两个对象:

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 ::关系的对象。的

推荐答案

使用merge:

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).