# Trouble wtih webstore schema, handling product variations

**URL:** <https://www.ruby-forum.com/t/trouble-wtih-webstore-schema-handling-product-variations/62675>\
**Category:** Rails\
**Created:** [June 7, 2006, 7:50am UTC](https://www.ruby-forum.com/t/trouble-wtih-webstore-schema-handling-product-variations/62675 "2006-06-07T07:50:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Peter\_Pumpkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/54ee81/32.png) [@Peter\_Peter\_Pumpkin](https://www.ruby-forum.com/u/Peter_Peter_Pumpkin)\
**Post date:** [June 7, 2006, 7:50am UTC](https://www.ruby-forum.com/t/trouble-wtih-webstore-schema-handling-product-variations/62675/1 "2006-06-07T07:50:34Z")

</div>

Hi Railers,

Does anyone have any guidance on setting up a simple way to handle  
product variations (e.g garment size/color)? Initially, I had thought  
that I would make a variation model which defined the extra bits, but it  
seems quite inflexible especially so if a store were to sell  
non-clothing items that also had variants of one flavor or another.

product  
has\_many variations  
title  
description  
price  
variations

variation  
belongs\_to garment  
has\_many images  
size  
color  
sku

would it make more sense for the variation model to subclass the product  
and add additional table columns?

As an aside, is anyone else having problems with the list and their  
GMail account? I have not received a message since 8h PST.

---

<div class="post-metadata">

**Author:** ![Peter\_Peter\_Pumpkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/54ee81/32.png) [@Peter\_Peter\_Pumpkin](https://www.ruby-forum.com/u/Peter_Peter_Pumpkin)\
**Post date:** [June 7, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/trouble-wtih-webstore-schema-handling-product-variations/62675/2 "2006-06-07T16:00:49Z")

</div>

I used STI for something slightly similar for something where each item  
was a type of model, but it had additional characteristics depending on  
whether it was a mainmodel or modelvariation (actually had three levels  
– parentmodel, mainmodel, and modelvariation).

class Modeltype \< ActiveRecord::Base  
…  
end  
class Modelvar \< Modeltype  
…  
end

I’ve then got a one-to-many self-referential relationship on the model  
to define the relationship.

However, re-reading your post, wondering if in your case you have a  
many-many relationship between products and possible product variations:  
a product can have many variations, and presumably some of those  
variations – e.g. extra large – can be associated with other products.

So what about  
variation  
HABTM products  
variation\_group (e.g. size, colour)  
variation\_specs (e.g. red, small)

I guess you could even make variation an STI if the variations required  
different details.

IF you used has\_many through the join table could contain the SKU.

Then again I could be talking rot – had a fruitless morning trying to  
sort out a mixin prob…

p.s. Yup, I’ve had the same prob with gmail. Seems OK now though.

---

<div class="post-metadata">

**Author:** ![Peter\_Peter\_Pumpkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/54ee81/32.png) [@Peter\_Peter\_Pumpkin](https://www.ruby-forum.com/u/Peter_Peter_Pumpkin)\
**Post date:** [June 7, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/trouble-wtih-webstore-schema-handling-product-variations/62675/3 "2006-06-07T17:14:47Z")

</div>

Hi Peter,

I am pretty much a newb, but I had basically the same problem. In my  
schema I have Products and 3 sub products that inherit from Products  
using STI. And I have an Options class (similar to your Variations)  
that belongs\_to one of the sub product classes (because the other two  
will never need options). What I did is create to different methods  
for adding items to a cart. The first is add\_to\_cart, the second is  
add\_option\_to\_cart. And an option contains a Product object or  
reference. Basically I did it this way because I just didn’t think  
that Options had enough in common with Products for them to inherit  
from Product.

I would however be interested in knowing if there’s a simpler way of  
doing this that doesn’t require using two different functions to add  
to the cart (and subsequently dealing with the items in the cart when  
it’s time to submit an order).

Here’s my models…

class Product \< ActiveRecord::Base  
has\_many :line\_items

```
 t.column "price_ca", :integer
 t.column "price_us", :integer
 t.column "catalog_number", :string
 t.column "type", :string
 t.column "form", :string
 t.column "order_number", :integer
 t.column "title", :string
 t.column "has_option", :boolean, :default => false, :null => false
 t.column "quantity", :integer
 t.column "created_at", :datetime
 t.column "updated_at", :datetime

```

end

class HardProduct \< Product  
has\_many :options  
end

class Option \< ActiveRecord::Base  
belongs\_to :hard\_product  
has\_many :line\_items

```
 t.column "hard_product_id", :integer
 t.column "description", :string
 t.column "quantity", :integer

```

end

Take care,  
Sean

---

<div class="post-metadata">

**Author:** ![Peter\_Peter\_Pumpkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/54ee81/32.png) [@Peter\_Peter\_Pumpkin](https://www.ruby-forum.com/u/Peter_Peter_Pumpkin)\
**Post date:** [June 8, 2006, 6:29am UTC](https://www.ruby-forum.com/t/trouble-wtih-webstore-schema-handling-product-variations/62675/4 "2006-06-08T06:29:41Z")

</div>

Thank you both, you have given me some good ideas on how to tackle this  
=)

Sean O’Hara wrote:

> Hi Peter,
> 
> I am pretty much a newb, but I had basically the same problem. In my  
> schema I have Products and 3 sub products that inherit from Products  
> using STI. And I have an Options class (similar to your Variations)  
> that belongs\_to one of the sub product classes (because the other two  
> will never need options). What I did is create to different methods  
> for adding items to a cart. The first is add\_to\_cart, the second is  
> add\_option\_to\_cart. And an option contains a Product object or  
> reference. Basically I did it this way because I just didn’t think  
> that Options had enough in common with Products for them to inherit  
> from Product.
