# Database Design for Rails Question

**URL:** <https://www.ruby-forum.com/t/database-design-for-rails-question/131493>\
**Category:** Rails\
**Created:** [March 10, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/database-design-for-rails-question/131493 "2008-03-10T17:35:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yanir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7ab992/32.png) [@Yanir](https://www.ruby-forum.com/u/Yanir)\
**Post date:** [March 10, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/database-design-for-rails-question/131493/1 "2008-03-10T17:35:49Z")

</div>

Hi all, I’ve just discovered Rails coming from an [asp.net](http://asp.net) background.  
I’m really into rails and I have started to develop an application to  
manage product data (specs, marketing info, etc). I am having some  
trouble designing the database to fit into the MVC paradigm and I was  
hoping someone might have a suggestion for me.

Here is my problem:

Let’s say I have one table with generic product model info in it:  
Product Table:  
modelnum:  
upc:  
type: refrigerator, range, etc  
price:

I also need a specs table. The catch is every product type has a  
different set of spec attributes.

How do i design my database without having a huge spec’s table full of  
unused colums, and logic in the view to display the appropriate spec  
fields.

I thought of perhaps using a table for each product type, but how  
would you make this association in the model without doing the  
following:

class Product \< ActiveRecord::Base  
has\_one :spec\_type1  
has\_one :spec\_type2  
has\_one :spec\_type3  
has\_one :spec\_type4

or generating sql strings to pull data from the correct table (which  
is something I would have probably done in asp)?

Alternativly I thought of representing specs with two tables.  
Specs\_def would define all of the available specifications available,  
and encode each spec\_attribute with a spec\_type code. A second table  
would contain all of the spec entries, creating a list of  
spec\_attribute/value pairs linked to the product by product\_id.

This also seems wrong, and messy when dealing with creating and  
updating product information.  
I’m sure there is a better way.

Any ideas would be greatly appreciated.

Yanir

---

<div class="post-metadata">

**Author:** ![Yanir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7ab992/32.png) [@Yanir](https://www.ruby-forum.com/u/Yanir)\
**Post date:** [March 10, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/database-design-for-rails-question/131493/2 "2008-03-10T18:04:15Z")

</div>

I think I found the answer: polymorphic associations.

---

<div class="post-metadata">

**Author:** ![Yanir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7ab992/32.png) [@Yanir](https://www.ruby-forum.com/u/Yanir)\
**Post date:** [March 10, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/database-design-for-rails-question/131493/3 "2008-03-10T20:27:36Z")

</div>

Sounds right.

---

<div class="post-metadata">

**Author:** ![Yanir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7ab992/32.png) [@Yanir](https://www.ruby-forum.com/u/Yanir)\
**Post date:** [March 10, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/database-design-for-rails-question/131493/4 "2008-03-10T21:01:02Z")

</div>

On Mar 10, 11:32 am, Yanir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all, I’ve just discovered Rails coming from an [asp.net](http://asp.net) background.

Welcome! I co-write a blog for people just like us:  
[www.softiesonrails.com](http://www.softiesonrails.com).  
You may (or may not) find it helpful as you learn Rails.

Jeff
