# Any ideas on how to model this?

**URL:** <https://www.ruby-forum.com/t/any-ideas-on-how-to-model-this/76009>\
**Category:** Rails\
**Created:** [November 15, 2006, 4:01am UTC](https://www.ruby-forum.com/t/any-ideas-on-how-to-model-this/76009 "2006-11-15T04:01:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![runrunshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@runrunshaw](https://www.ruby-forum.com/u/runrunshaw)\
**Post date:** [November 15, 2006, 4:01am UTC](https://www.ruby-forum.com/t/any-ideas-on-how-to-model-this/76009/1 "2006-11-15T04:01:49Z")

</div>

Hello,

I’m attempting to move beyond the Agile Web Dev w\Rails book and explore  
writing some practice apps from scratch. I m attempting to model an  
online t-shirt shop were customers can select from a variety of  
different designs. Each design will be available in different colors  
and each color will have different sizes. Also, I’d like to keep track  
of the number of available shirts.

My most recent data modeling attempt consists of 3 tables and respective  
columns:

1. designs - name, description, picture, price.
2. color - color, design\_id
3. size - size, color\_id

The model associations are:  
design - has\_many :colors, has\_many :sizes  
color - has\_many :sizes, belongs\_to :designs  
size - belongs\_to :color, :belongs\_to :design

SO far, so good! The problem is having to keep an inventory of the  
number of available shirts (i.e. quantity). I have a 4th table called  
‘quantity’ which has the following columns: quantity, design\_id,  
color\_id, size\_id since it isn’t strictly associated with a single  
design, color or size but all three.

But… From here I’m lost. I have tried using a HABTM association  
between the design table and quantity table but something just didn’t  
seem right. So I’m asking for help. What is the best way to model  
this? Does the ‘quantity’ table need to be on it’s own or can I perhaps  
rework it into the other 3 tables?

Any help is greatly appreciated!!!

---

<div class="post-metadata">

**Author:** ![runrunshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@runrunshaw](https://www.ruby-forum.com/u/runrunshaw)\
**Post date:** [November 15, 2006, 4:22am UTC](https://www.ruby-forum.com/t/any-ideas-on-how-to-model-this/76009/2 "2006-11-15T04:22:50Z")

</div>

Hi Richard,

Richard D. wrote:

> I m attempting to model an online t-shirt shop  
>   
> My most recent data modeling attempt consists  
> of 3 tables and respective columns:
> 
> 1. designs - name, description, picture, price.
> 2. color - color, design\_id
> 3. size - size, color\_id
> 
> The model associations are:

… largely irrelevant until you have all the models you need. If  
you’re  
modeling a t-shirt shop, one might expect a t-shirt model. That might  
have  
a quantity-on-hand attribute. Of course, that’s just one way to look at  
it.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![runrunshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@runrunshaw](https://www.ruby-forum.com/u/runrunshaw)\
**Post date:** [November 15, 2006, 11:13pm UTC](https://www.ruby-forum.com/t/any-ideas-on-how-to-model-this/76009/3 "2006-11-15T23:13:59Z")

</div>

On 11/15/06, Richard D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My most recent data modeling attempt consists of 3 tables and respective  
> columns:
> 
> 1. designs - name, description, picture, price.
> 2. color - color, design\_id
> 3. size - size, color\_id
> 
> The model associations are:  
> design - has\_many :colors, has\_many :sizes  
> color - has\_many :sizes, belongs\_to :designs  
> size - belongs\_to :color, :belongs\_to :design

The relationships are not right. You can break them easily by creating  
a color belonging\_to one design, and a size belonging to the same  
color, but a different design. If you drew some sort of model diagram,  
you would see that you have created a triangle between the three  
entities, which is (almost) always a Very Bad Idea. You would also  
have to model the same color (e.g. Blue) multiple times - one for each  
design; and model each size (S/M/L) once for each color/design  
combination.

You have a need for an explicit join model. There are a few different  
ways of modelling this, but let’s introduce a new model class calle  
Variation - this is a fairly common term in this kind of situation.  
Normally, each Variation of a product in a shop will have it’s own  
unique product number of some sort.  
Your model might look like this:

Variation  
belongs\_to :design  
belongs\_to :color  
belongs\_to :size

Design  
has\_many :variations  
has\_many :colors, :through=\>:variations  
has\_many :sizes, :through=\>:variations

This way, while logically your Design model offers the same access to  
the colors and sizes, you have created a _derived_ relationship  
between the classes, which is much preferrable to the Triangle Of  
Doom.

To get a list of colors for a chosen design is simple:  
some\_design.colors

To get a list of all variations for a design and a color (from which  
you can get the sizes):  
some\_design.variations.find\_by\_color\_id(the\_color\_id)

Or get the sizes directly:  
some\_design.variations.find\_by\_color\_id(the\_color\_id).collect { |v|  
v.size }

The last two should probably be implemented as methods on the Design  
class (get\_variations\_for\_color(color), get\_sizes\_for\_color(color))

You could then ad the stock quantities to the Variaion model, or model  
them in a separate table with a has\_one relationship.

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![runrunshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@runrunshaw](https://www.ruby-forum.com/u/runrunshaw)\
**Post date:** [November 16, 2006, 12:52am UTC](https://www.ruby-forum.com/t/any-ideas-on-how-to-model-this/76009/4 "2006-11-16T00:52:15Z")

</div>

Awesome info Max! I’m working on modeling a scenario of my own and  
although it’s a little bit different from this one, reading your post  
on how to tackle this problem was VERY insightful.

---

<div class="post-metadata">

**Author:** ![runrunshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@runrunshaw](https://www.ruby-forum.com/u/runrunshaw)\
**Post date:** [November 17, 2006, 3:49am UTC](https://www.ruby-forum.com/t/any-ideas-on-how-to-model-this/76009/5 "2006-11-17T03:49:54Z")

</div>

Thank you very much. This is just what I was looking for!!!
