Rails - filter using join model on has_many through
So you want to filter using a scope through an association (not just for use on has_many through, but I feel this example really shows how #merge shines!).
Given the following models:
class Person
has_many :accounts
has_many :computers, through: :accounts
end
class Account
belongs_to :person
belongs_to :computer
scope :administrators, -> { where(role: 'administrator') }
end
class Computer
has_many :accounts
has_many :people, through: :accounts
end
Filtering using a join model on a has_many through can be naively accomplished with the following. Here assume we have a person instance and want to find all computers where they are an administrator.
person.accounts.administrators.map(&:computer) # ewww
We can do this better using ActiveRecord::SpawnMethods#merge!
person.computers.merge(Account.administrators) # very nice!
Cleaner syntax, claner query. Win win
Written by James Brooks
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#