Reputation: 5293
What is an elegant way to define model, so one product can have different sizes (and those sizes would have different prices)?
Should I create something like this:
rails generate model Product name:string
rails generate model ProductSize size:string product:references
rails generate model SizePrice price:decimal size:references
?
I have been looking for an answer but I have not found anything that newbie like me would understand.
Upvotes: 1
Views: 56
Reputation: 5217
You are on the right track. You need to implement a many-to-many connection using a join table (in your case it would be the SizePrice table). See section 2.4 of this Rails Guide.
Upvotes: 1