Adding Two Activerecord::Relation Objects

Combine two ActiveRecord::Relation objects

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.

adding two ActiveRecord::Relation objects

Try:

new_relation = relation.merge(another_relation)

Rails Active Record Query to push two active record relation objects

This will work, but makes at least three SQL calls:

first_user = User.first
last_user = User.last
users = User.where(id: [first_user.id, last_user.id]).paginate

Combine two ActiveRecord Query results

Use relation & relation:

Model.joins("join relationships ON user_id = followed_id").where("follower_id = {user.id}") & Model.where(:user_id => user.id)


Related Topics



Leave a reply



Submit