Created
March 2, 2015 18:25
-
-
Save coalwater/5a6bb89670424b63c6fd to your computer and use it in GitHub Desktop.
Using active record's #merge to separate concerns
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 User < ActiveRecord::Base | |
has_many :dvds | |
end | |
class Dvd < ActiveRecord::Base | |
belongs_to :user | |
end |
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 Dvd < ActiveRecord::Base | |
belongs_to :user | |
scope :unreturned, -> { where(returned: false) } | |
end |
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 User < ActiveRecord::Base | |
scope :with_unreturned_dvds, joins(:dvd).where(dvd: { returned: false }) | |
end |
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 User < ActiveRecord::Base | |
scope :with_unreturned_dvds, joins(:dvd).merge(Dvd.unreturned) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment