Created
April 26, 2017 07:04
-
-
Save edwinlab/f19219e56e686ac84f364b5b139e7160 to your computer and use it in GitHub Desktop.
filter using join model on has_many through
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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