Database Design for Rails Question

Hi all, I've just discovered Rails coming from an 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.


I think I found the answer: polymorphic associations.

Sounds right.

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