# Modeling a Database

**URL:** <https://www.ruby-forum.com/t/modeling-a-database/119325>\
**Category:** Rails\
**Created:** [November 6, 2007, 1:34am UTC](https://www.ruby-forum.com/t/modeling-a-database/119325 "2007-11-06T01:34:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![seank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@seank](https://www.ruby-forum.com/u/seank)\
**Post date:** [November 6, 2007, 1:34am UTC](https://www.ruby-forum.com/t/modeling-a-database/119325/1 "2007-11-06T01:34:44Z")

</div>

All of the rails tutorials I’ve read show how to create simple table  
relationships within a database.  
For example: A store model relating to a product model through a join  
table.

However, in the application I’m building I’d like to do something  
slightly more complex. I’d like each store model to have one or more  
custom, unique product models.

I’m not quite sure how to model this relationship. Any help would be  
greatly appreciated.

---

<div class="post-metadata">

**Author:** ![seank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@seank](https://www.ruby-forum.com/u/seank)\
**Post date:** [November 6, 2007, 1:48am UTC](https://www.ruby-forum.com/t/modeling-a-database/119325/2 "2007-11-06T01:48:10Z")

</div>

This isn’t really a Rails-specific problem. If you can model out your  
application using database tables, then you can do it inRails. If you  
can’t,  
then it might be too complex and you might need to rethink it.

Perhaps you could give a more concrete example of what you want to build  
and  
someone can give some suggestions on how to approach it.

---

<div class="post-metadata">

**Author:** ![seank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@seank](https://www.ruby-forum.com/u/seank)\
**Post date:** [November 6, 2007, 2:13am UTC](https://www.ruby-forum.com/t/modeling-a-database/119325/3 "2007-11-06T02:13:52Z")

</div>

Here’s what a simple example would look like:

Store Table:  
id =\> 1; name =\> ‘Store 1’  
id =\> 2; name =\> ‘Store 2’

Product 1 Table:  
id =\> 1; name =\> ‘Jeans’; desc =\> ‘Denim jeans’; color =\> ‘blue’;  
label =\> ‘High Street Clothing’

Product 2 Table:  
id =\> 1; name =\> ‘Wine’; desc =\> ‘red wine’; region =\> ‘sonoma’,  
flavor =\> ‘great’

Then I might want to relate ‘Store 1’ to items in the Product 1 Table  
and ‘Store 2’ to items in the Product 2 Table.

I apologize in advance if that isn’t easy to understand.

---

<div class="post-metadata">

**Author:** ![seank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@seank](https://www.ruby-forum.com/u/seank)\
**Post date:** [November 6, 2007, 3:27am UTC](https://www.ruby-forum.com/t/modeling-a-database/119325/4 "2007-11-06T03:27:27Z")

</div>

There are two possibilities here, both of which are handled by Rails  
quite nicely.

Option 1: You could use Single Table Inheritance (STI) to model your  
products. This is good if most of the attributes on products are in  
common to all products with a few custom ones added to subclasses of  
Product.

Option 2: Polymorphic relationships. This can be a better choice if  
your products vary greatly in their attributes.

Come to think of it, there is a third option that may work nicely:  
Rather than try to store all the various attributes of each individual  
product within the products table you could use something like  
acts\_as\_taggable to tag the attributes to your products.

There are a limited number of possible relationship types that can go  
into any RDBMS. Rails handles them all, so as mentioned before it’s  
not really a Rails question, it’s a basic RDBMS question.

Note: There are some things a bit “frowned upon” in Rails, not because  
they can’t be done, but that they can make your life more complicated.  
Namely relationships using compound primary keys. This is just bad  
practice in any object relational mapping system (in my opinion  
anyway).

---

<div class="post-metadata">

**Author:** ![seank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@seank](https://www.ruby-forum.com/u/seank)\
**Post date:** [November 6, 2007, 6:03am UTC](https://www.ruby-forum.com/t/modeling-a-database/119325/5 "2007-11-06T06:03:30Z")

</div>

Thank you Robert. The documentation on polymorphic relationships looks  
promising.

---

<div class="post-metadata">

**Author:** ![seank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@seank](https://www.ruby-forum.com/u/seank)\
**Post date:** [November 6, 2007, 7:02am UTC](https://www.ruby-forum.com/t/modeling-a-database/119325/6 "2007-11-06T07:02:30Z")

</div>

On Nov 5, 7:13 pm, seank [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Product 2 Table:  
> id =\> 1; name =\> ‘Wine’; desc =\> ‘red wine’; region =\> ‘sonoma’,  
> flavor =\> ‘great’
> 
> Then I might want to relate ‘Store 1’ to items in the Product 1 Table  
> and ‘Store 2’ to items in the Product 2 Table.
> 
> I apologize in advance if that isn’t easy to understand.

Maybe I’m missing something, but I think you just need one Products  
table, with a store\_id column.

class Store \< ActiveRecord::Base  
has\_many :products  
end

class Product \< ActiveRecord::Base  
belongs\_to :store  
end

s = Store.find\_by\_name(‘Store 1’)  
s.products # returns a list of all products in Store 1

Does this help?

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![seank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@seank](https://www.ruby-forum.com/u/seank)\
**Post date:** [November 6, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/modeling-a-database/119325/7 "2007-11-06T17:40:39Z")

</div>

Jeff, the complication comes from the fact that each store will have  
different product models.  
For example, one store might sell clothes, another might sell  
furniture, and another might sell beverages.  
A generic product model for all the stores won’t offer enough detail.  
The clothing store will need details such as sizes, fabric, colors,  
etc.  
The furniture store will need width, length, height, wood type, etc.

---

<div class="post-metadata">

**Author:** ![seank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@seank](https://www.ruby-forum.com/u/seank)\
**Post date:** [November 6, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/modeling-a-database/119325/8 "2007-11-06T22:48:08Z")

</div>

On Nov 6, 10:39 am, seank [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Jeff, the complication comes from the fact that each store will have  
> different product models.  
> For example, one store might sell clothes, another might sell  
> furniture, and another might sell beverages.  
> A generic product model for all the stores won’t offer enough detail.  
> The clothing store will need details such as sizes, fabric, colors,  
> etc.  
> The furniture store will need width, length, height, wood type, etc.

Ah, I see. Yes, polymorphic associations are probably the answer. I  
found the explanation in Agile Web D. with Rails (2nd Ed.) to  
be really helpful (I’m sure there are other good resources too).

If it’s still not clear, let me know and I’ll try to provide more  
details.

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![seank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@seank](https://www.ruby-forum.com/u/seank)\
**Post date:** [November 10, 2007, 1:13am UTC](https://www.ruby-forum.com/t/modeling-a-database/119325/9 "2007-11-10T01:13:01Z")

</div>

Okay, I finally figured it out.

For any future travelers the following links were helpful:

“How To Use Polymorphic Associations”  
[http://wiki.rubyonrails.org/rails/pages/HowToUsePolymorphicAssociations](http://wiki.rubyonrails.org/rails/pages/HowToUsePolymorphicAssociations)

Polymorphic associations have an inherent limitation that Josh S.  
does a great job of covering:  
“The other side of polymorphic :through associations”  
[http://blog.hasmanythrough.com/2006/04/03/polymorphic-through](http://blog.hasmanythrough.com/2006/04/03/polymorphic-through)
