Skip to content

Instantly share code, notes, and snippets.

@vzaremba
Last active December 23, 2015 00:09
Show Gist options
  • Save vzaremba/6551391 to your computer and use it in GitHub Desktop.
Save vzaremba/6551391 to your computer and use it in GitHub Desktop.
Rails - filter using join model on has_many through.
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
person.computers.merge(Account.administrators) # very nice!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment