On page 350 in the PDF it mentions that having multiple tables have a has_one relationship to the same table cannot work, that is, class Article has_one :catalog_entry end class Sound has_one :catalog_entry end class Image has_one :catalog_entry end I quote, "We can''t possibly arrange to have the foreign key in the catalog entry point back to all three tables" If I understand correctly, it says catalog_entry cannot contain a reference to all three tables. My question is, why not? If catalog_entry has two foreign keys, image_id and sound_id, wouldn''t all the following calls work? Image.catalog_entry Sound.catalog_entry CatalogEntry.image CatalogEntry.sound I cannot see the problem the book suggests is there, what am I missing? -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan
2007-Jun-02 08:47 UTC
Re: Polymorphic associations. Agile rails book confusion.
Michael Chow wrote:> On page 350 in the PDF it mentions that having multiple tables have a > has_one relationship to the same table cannot work, that is, > > class Article > has_one :catalog_entry> > Image.catalog_entry > Sound.catalog_entry > CatalogEntry.image > CatalogEntry.sound > > I cannot see the problem the book suggests is there, what am I missing?You have to use blongs_to when you use has_one, that''s why it''s not working :) -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Michael Chow
2007-Jun-02 09:53 UTC
Re: Polymorphic associations. Agile rails book confusion.
Jamal Soueidan wrote:> You have to use blongs_to when you use has_one, that''s why it''s not > working :)Oh sure, I know, but... class CatalogEntry belongs_to :image belongs_to :sound belongs_to :article end I''ve not used multiple belongs_to associations in a single class yet, but I can''t see why this wouldn''t work as long as there are foreign keys for each one. In this case the catalog entry would be pointing to all three tables, would it not? -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan
2007-Jun-02 10:35 UTC
Re: Polymorphic associations. Agile rails book confusion.
Michael Chow wrote:> Jamal Soueidan wrote: >> You have to use blongs_to when you use has_one, that''s why it''s not >> working :) > > I''ve not used multiple belongs_to associations in a single class yet, > but I can''t see why this wouldn''t work as long as there are foreign keys > for each one. > > In this case the catalog entry would be pointing to all three tables, > would it not?Can I see your tables :) class Blog has_many :comments end class Comments belongs_to :blog end The comments table MUST have a columns named "blog_id", or else the association would not work. -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
I think the goal is to get something like CatalogEntry.resource to accept or return a number of different objects. If polymorphic associations are being used than we could do something like this: CatalogEntry.resource = Image.new or CatalogEntry.resource = Sound.new or CatalogEntry.resource = Video.new Normal associations won''t allow that. Jim On Jun 1, 10:51 pm, Michael Chow <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> On page 350 in the PDF it mentions that having multiple tables have a > has_one relationship to the same table cannot work, that is, > > class Article > has_one :catalog_entry > end > > class Sound > has_one :catalog_entry > end > > class Image > has_one :catalog_entry > end > > I quote, "We can''t possibly arrange to have the foreign key in the > catalog entry point back to all three tables" > > If I understand correctly, it says catalog_entry cannot contain a > reference to all three tables. My question is, why not? > > If catalog_entry has two foreign keys, image_id and sound_id, wouldn''t > all the following calls work? > > Image.catalog_entry > Sound.catalog_entry > CatalogEntry.image > CatalogEntry.sound > > I cannot see the problem the book suggests is there, what am I missing? > > -- > Posted viahttp://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan
2007-Jun-02 16:04 UTC
Re: Polymorphic associations. Agile rails book confusion.
Understanding Polymorphic Associations http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations You get some examples how it works :) -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Michael Chow
2007-Jun-02 20:56 UTC
Re: Polymorphic associations. Agile rails book confusion.
Its really the "We cant possibly arrange to have the foreign key in the catalog entry point back to all three tables" part that confused me. As far as I can see, you can point back to all three, you just have to add more foreign keys... But I think I''m starting to see the point, the idea is to point back to either of the three without using more foreign keys. That is, so you can access the resource without necessarily knowing what the resource is. If I''m making sense of this correctly, it would make better sense to use regular associations if the catalog entry is associated with all three classes, but use polymorphic if it can be either of the three. If I have this all backwards, let me know, otherwise thanks :) -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Bill Kocik
2007-Jun-02 22:16 UTC
Re: Polymorphic associations. Agile rails book confusion.
On 6/2/07, Michael Chow <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> If I''m making sense of this correctly, it would make better sense to use > regular associations if the catalog entry is associated with all three > classes, but use polymorphic if it can be either of the three. If I have > this all backwards, let me know, otherwise thanks :)Nope - looks like you''ve got it. To clear up your initial confusion, when they say "can''t arrange to have the foreign key point back to all three tables", think of it as "can''t arrange to have *one single* foreign key point back to all three tables". Of course you can do it with three foreign keys, but as you now understand, that isn''t the point of polymorphic associations. -- Bill Kocik http://bkocik.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---