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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On 11/5/07, seank <skenney26-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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). On Nov 5, 8:13 pm, seank <skenne...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you Robert. The documentation on polymorphic relationships looks promising. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 5, 7:13 pm, seank <skenne...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.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 softiesonrails.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 6, 10:39 am, seank <skenne...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 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. Jeff softiesonrails.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Polymorphic associations have an inherent limitation that Josh Susser does a great job of covering: "The other side of polymorphic :through associations" http://blog.hasmanythrough.com/2006/04/03/polymorphic-through --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---