mdmb
mdmb

Reputation: 5293

Rails/ MySQL - Product with sizes and prices

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

Answers (1)

Mick
Mick

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

Related Questions