12

I have an ecommerce store that I am building. I am using Rails/ActiveRecord, but that really isn't necessary to answer this question (however, if you are familiar with those things, please feel free to answer in terms of Rails/AR).

One of the store's requirements is that it needs to represent two types of products:

  1. Simple products - these are products that just have one option, such as a band's CD. It has a basic price, and quantity.
  2. Products with variation - these are products that have multiple options, such as a t-shirt that has 3 sizes and 3 colors. Each combination of size and color would have its own price and quantity.

I have done this kind of thing in the past, and done the following:

  • Have a products table, which has the main information for the product (title, etc).
  • Have a variants table, which holds the price and quantity information for each type of variant. Products have_many Variants.
  • For simple products, they would just have one associated Variant.

Are there better ways I could be doing this?

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
David Balatero
  • 121
  • 1
  • 3

2 Answers2

11

I worked on an e-commerce product a few years ago, and we did it the way you described. But we added one more layer to handle multiple attributes on the same product (size and color, like you said). We tracked each attribute separately, and we had a "SKUs" table that listed each attribute combination that was allowed for each product. Something like this:

attr_id   attr_name  
1         Size  
2         Color  

sku_id    prod_id    attr_id    attr_val  
1         1          1          Small  
1         1          2          Blue  
2         1          1          Small  
2         1          2          Red  
3         1          1          Large  
3         1          2          Red  

Later, we added inventory tracking and other features, and we tied them to the sku IDs so that we could track each one separately.

Josh Yeager
  • 3,763
  • 1
  • 25
  • 29
  • What if the product didn't have any attributes? Would attr_id be null? Or did you ensure every product had at least one attribute? – Joe Van Dyk Jun 01 '12 at 23:01
  • 1
    In that case, that product didn't have any rows in the SKU table. – Josh Yeager Jun 12 '12 at 15:24
  • What about wildcards? A variation for "Small" where size is undefined? – Chris Pillen Jun 07 '15 at 14:28
  • hi Josh, is this not `Entity Attribute Value` pattern, i want to use this but can you check my question [here](http://stackoverflow.com/questions/31827767/asp-mvc-list-ecommerce-product-with-dynamically-changing-specifications) ? – Shaiju T Aug 05 '15 at 09:17
0

Your way seems pretty flexible. It would be similar to my first cut.

John
  • 15,990
  • 10
  • 70
  • 110