For a project I'm working on, the store has two types of products - a real product and a group of products.
For this discussion, let's call them "1 T shirt" and "a box of T shirts". For one t-shirt, I need to store the normal attributes - price, sku, size, color, description, etc. For the box of t-shirts I need to have a price, sku, description, and a list of t-shirts that are included.
So right now, I'm representing this with the Shirt and ShirtCollection models. I can see this causing difficulty down the road when I need to do reporting and order management and making sure SKUs are unique.
So what's the best way of representing this?
You can have a Tshirt table and then self reference it with a has_many :through association.
Tshirt - id, sku, price, size, color, description, is_box
TshirtBox - parent_tshirt (id that references tshirt), child_tshirt (id that references tshirt)
Check out this link for more on self referential has_many :through http://www.aldenta.com/2006/11/10/has_many-through-self-referential-example/
I would have the following models
Tshirt
TshirtBox has_many TshirtItems
TshirtBoxItems (This is basically a join table with an id tshirt_box_id and tshirt_id) belongs_to TshirtBox
TshirtBoxItems is a way to link a Tshirt with a box and potentially other things in the future.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.