I have 3 models. These are the User, Product and Price model. I want Users to be able to subscribe to Products and watch the Price model for lower prices. I’m thinking about how to set this up but haven’t figured out the correct associations.
My associations as of now are like this:
Note: Many fields (table columns as well) have been erased for simplicity
class User
has_many :prices, :dependent => :destroy
has_many :products, :through => :prices
end
class Product
# table columns - :name
has_many :prices
has_many :users, :through => :prices
end
class Price
# table columns - :cost, :user_id, :product_id, :store_id
belongs_to :user
belongs_to :product
belongs_to :store
end
I was going to set this up by making a boolean in the Product model called :watch_product but I get stuck at the associations. Should I give Product a user_id or the other way around? I have a :through association but no user_id on Product or a product_id on User. Should the watch_product go on the Price instead?
I would not store the user information in the
Pricemodel. I would introduce a separate model for storing user to product subscription.Now you can get the prices for user as follows: