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
Use Pry in Gems Without Modifying The Gemfile or Using 'Require'
Deleting a Specific Line in a Text File
Sinatra Not Persisting Session with Redirect on Chrome
How to Troubleshoot Memory Bloat at Boot for Rails App
Ruby: Converting a Nested Ruby Hash to an Un-Nested One
Unit Testing Code Which Gets Current Time
How to Read Text from Non Visible Elements with Watir (Ruby)
Can't Get Paypal Encrypted Website Payments to Work in Rails
Rails: What's Wrong with This Multiple Join with Conditions on The Associations
Can't Log into Active Admin. Any Way to Create an Admin User
How to Directly Install a Gem from a Git Repository
Rails Validating Search Params
How Can Multiple Trailing Slashes Can Be Removed from a Url in Ruby
Parsing a CSV File Using Different Encodings and Libraries
Activerecord Join Table for Legacy Database
What Is Returned in Ruby If The Last Statement Evaluated Is an If Statement