Skip to content

Instantly share code, notes, and snippets.

@zmajstor
Created February 10, 2014 11:20
Show Gist options
  • Save zmajstor/8914214 to your computer and use it in GitHub Desktop.
Save zmajstor/8914214 to your computer and use it in GitHub Desktop.
AR DRY scoping of the nested models
class Collection < ActiveRecord::Base
has_many :products
scope :live, where("collections.end_date IS NOT NULL AND collections.end_date >= ?", Time.now.to_date)
end
class Product < ActiveRecord::Base
belongs_to :collection
# how to DRY this .where ?
scope :launched, joins(:collection).where("collections.end_date IS NOT NULL AND collections.end_date >= ?", Time.now.to_date)
end
# usage:
Product.launched
# Product scope which belongs_to Collection.live
@zmajstor
Copy link
Author

da, znam za lambda, ali, naravno da smetnem s uma kad najviše treba ... :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment