I need to DRY up some of my tables. Here is the situation: I have a number of objects that habtm name objects. What I need is some kind of polymorphic join model so each object can relate to the same names table. An example: oranges table orange row orange row ... apples table apple row apple row ... names table valencia red delicious gala navel blood orange ... I need to break the names out because some of the oranges or apples have different names, but the same other data. Am I stuck with a names table and join model for each fruit, or can I do something tricky with has_many :through and meta-data? TIA, --Dean --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
What about single-table inheritance (STI)? It would allow you do something like the following: fruits table - name - color - type (Orange, Apple, etc.) class Fruit < ActiveRecord::Base; end class Apple < Fruit; end class Orange < Fruit; end More information here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html http://wiki.rubyonrails.org/rails/pages/SingleTableInheritance http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html On 2/12/07, Dean <dean.brundage-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I need to DRY up some of my tables. Here is the situation: I have a > number of objects that habtm name objects. What I need is some kind > of polymorphic join model so each object can relate to the same names > table. An example: > > oranges table > orange row > orange row > ... > > apples table > apple row > apple row > ... > > names table > valencia > red delicious > gala > navel > blood orange > ... > > I need to break the names out because some of the oranges or apples > have different names, but the same other data. Am I stuck with a > names table and join model for each fruit, or can I do something > tricky with has_many :through and meta-data? > TIA, > --Dean > > > > >-- Michael Guterl --~--~---------~--~----~------------~-------~--~----~ 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 Feb 12, 10:45 am, "Michael Guterl" <mgut...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> What about single-table inheritance (STI)? It would allow you do something > like the following: > > fruits table > - name > - color > - type (Orange, Apple, etc.) > > class Fruit < ActiveRecord::Base; end > class Apple < Fruit; end > class Orange < Fruit; endI thought about that, but I have way too much information about my "fruits" plus five different types of "fruit" to mash into one table. I may have to hack together some cool join model. --Dean> More information here:http://api.rubyonrails.org/classes/ActiveRecord/Base.htmlhttp://wiki.rubyonrails.org/rails/pages/SingleTableInheritancehttp://www.martinfowler.com/eaaCatalog/singleTableInheritance.html > > On 2/12/07, Dean <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > I need to DRY up some of my tables. Here is the situation: I have a > > number of objects that habtm name objects. What I need is some kind > > of polymorphic join model so each object can relate to the same names > > table. An example: > > > oranges table > > orange row > > orange row > > ... > > > apples table > > apple row > > apple row > > ... > > > names table > > valencia > > red delicious > > gala > > navel > > blood orange > > ... > > > I need to break the names out because some of the oranges or apples > > have different names, but the same other data. Am I stuck with a > > names table and join model for each fruit, or can I do something > > tricky with has_many :through and meta-data? > > TIA, > > --Dean > > -- > Michael Guterl--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Don''t try to break shared columns out into their own table. It''s a significant complication for no performance gain. Just use Single Table Inheritance or Polymorphic Associations, dpeending on what you''re trying to do. We can''t tell what''s appropriate, because you only described your twisted data structure, without telling us what you''re trying to model. :-) Ciao, Sheldon. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
P.S.: It''s probably a mistake to produce one class per type of fruit. Chances are, you can model them all using a single Fruit class. Encapsulate the concept that varies. Ciao, Sheldon. --~--~---------~--~----~------------~-------~--~----~ 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 Feb 13, 12:16 am, "Sheldon Hearn" <sheld...-Uqlk7TKVmMlWaveUIWBRJg@public.gmane.org> wrote:> P.S.: It''s probably a mistake to produce one class per type of fruit. > Chances are, you can model them all using a single Fruit class. > > Encapsulate the concept that varies. > > Ciao, > Sheldon.Sorry for the obfuscation, I am just being paranoid. I am modeling ingredients to a recipe for making beer. There are five things that go into beer: fermentables, hops, extras, yeast and water. Each ingredient provides different things to a recipe. For example, the join model between recipes and fermentables contains an amount, potential and color. Hops provide boil time, amount, bitterness and form. Each ingredient has different information in the join model. Furthermore, if I made a single table out of all five ingredient types it would have between 60 and 75 columns. I was able to build a single controller for all 5 ingredients until I came to the hops. Some of them have different names, so that meant using a specialized controller just for them. I would like to get all my ingredients modeled the same way so I can avoid that. Thanks for the help. --Dean --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ah, okay. So it sounds like you''d be very well served indeed by a polymorphic association. --~--~---------~--~----~------------~-------~--~----~ 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 Feb 16, 5:36 am, "Sheldon Hearn" <sheld...-Uqlk7TKVmMlWaveUIWBRJg@public.gmane.org> wrote:> Ah, okay. So it sounds like you''d be very well served indeed by a > polymorphic association.:-D And I will run into the same problem when I go to implement the Retailers model and who sells which ingredient for how much. Gah! Could someone tell me what would be involved in creating a polymorphic join model? I have enough knowledge of Ruby to kludge something together by copying from the available polymorphic associations. Is a polymorphic join model actually feasible? Will anyone else find this useful? Regards, --Dean --~--~---------~--~----~------------~-------~--~----~ 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 Feb 18, 7:03 pm, "Dean" <dean.brund...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Could someone tell me what would be involved in creating a polymorphic > join model? I have enough knowledge of Ruby to kludge something > together by copying from the available polymorphic associations.Looks like Evan Weaver has already implemented this. http://blog.evanweaver.com/pages/has_many_polymorphs I will take a closer look. Thanks to Evan. --Dean --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---