Combine two ActiveRecord::Relation objects
arel, rails-activerecord, ruby-on-rails
Solution
If you want to combine using `AND` (intersection), use `merge`:
first_name_relation.merge(last_name_relation)
If you want to combine using `OR` (union), use `or`†:
first_name_relation.or(last_name_relation)
† Only in ActiveRecord 5+; for 4.2 install the where-or backport.
Problem
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 ``` is it possible to combine the two relations to produce one `ActiveRecord::Relation` object containing both conditions? Note: I'm aware that I can chain the wheres to get this behavior, what I'm really interested in is the case where I have two separate `ActiveRecord::Relation` objects.