Skip to content

Instantly share code, notes, and snippets.

@edwinlab
Created April 26, 2017 07:04
Show Gist options
  • Select an option

  • Save edwinlab/f19219e56e686ac84f364b5b139e7160 to your computer and use it in GitHub Desktop.

Select an option

Save edwinlab/f19219e56e686ac84f364b5b139e7160 to your computer and use it in GitHub Desktop.
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.accounts.administrators.map(&:computer) # ewww
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