Skip to content

Instantly share code, notes, and snippets.

@coalwater
Created March 2, 2015 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coalwater/5a6bb89670424b63c6fd to your computer and use it in GitHub Desktop.
Save coalwater/5a6bb89670424b63c6fd to your computer and use it in GitHub Desktop.
Using active record's #merge to separate concerns
class User < ActiveRecord::Base
has_many :dvds
end
class Dvd < ActiveRecord::Base
belongs_to :user
end
class Dvd < ActiveRecord::Base
belongs_to :user
scope :unreturned, -> { where(returned: false) }
end
class User < ActiveRecord::Base
scope :with_unreturned_dvds, joins(:dvd).where(dvd: { returned: false })
end
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