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.
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.
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).
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 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
details.