Modeling a Database

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

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.

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.

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.

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

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

Thank you Robert. The documentation on polymorphic relationships looks

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

class Product < ActiveRecord::Base
  belongs_to :store

s = Store.find_by_name('Store 1')
s.products # returns a list of all products in Store 1

Does this help?


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,
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 Development 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


Okay, I finally figured it out.

For any future travelers the following links were helpful:

"How To Use Polymorphic Associations"

Polymorphic associations have an inherent limitation that Josh Susser
does a great job of covering:
"The other side of polymorphic :through associations"