Skip to content

Instantly share code, notes, and snippets.

@nz
Forked from acadavid/gist:945741
Created April 28, 2011 03:27
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 nz/945742 to your computer and use it in GitHub Desktop.
Save nz/945742 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
has_many :product_interests
has_many :products, :through => :product_interests
has_many :brand_interests
has_many :brands, :through => :brand_interests
def deals
product_interests.collect(&:deals) + brand_interests.collect(&:deals)
end
end
class Deal < ActiveRecord::Base
belongs_to :brand
belongs_to :product
def interested_users
brand.users
end
end
class Product
has_many :deals
has_many :product_interests
has_many :users, :through => :product_interests
end
class Brand
has_many :deals
has_many :brand_interests
has_many :users, :through => :brand_interests
end
class ProductInterest
belong_to :user
belongs_to :product
delegate :deals, :to => :product
end
class BrandInterest
belong_to :user
belongs_to :brand
delegate :deals, :to => :brand
end
@user.deals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment