I''ve been googling this for the last day, and right now all I''ve got is a headache. There are several articles on this - but they are not making sense. One is using a field :association_foreign_key that I can''t find in my documentation. I admit it, I''m lost. Assume I have a Categories table. To make it simple, give it one field, a name. A category can be at the top level (no parents) A category can be subordinate to another category (it''s child - and obviously, the first category is the parent. This is a self-referential many-to-many. OK, I know that I need a join table - for grins, lets call that Relationship with two fields: category_id and parent_id. This isn''t carved in stone. If I need another layout - and it works - I''m happy as I can be. What models do I need, and how do I define these relationships? Can anyone help here. Thanks much ---Michael -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Interesting to read: http://guides.rails.info/association_basics.html#self-joins Op 18 jun 2010, om 19:53 heeft Michael Satterwhite het volgende geschreven:> I''ve been googling this for the last day, and right now all I''ve got is > a headache. There are several articles on this - but they are not making > sense. One is using a field :association_foreign_key that I can''t find > in my documentation. I admit it, I''m lost. > > Assume I have a Categories table. To make it simple, give it one field, > a name. > A category can be at the top level (no parents) > A category can be subordinate to another category (it''s child - and > obviously, the first category is the parent. This is a self-referential > many-to-many. > > OK, I know that I need a join table - for grins, lets call that > Relationship with two fields: category_id and parent_id. This isn''t > carved in stone. If I need another layout - and it works - I''m happy as > I can be. > > What models do I need, and how do I define these relationships? Can > anyone help here. > > Thanks much > ---Michael > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Satterwhite wrote:> I''ve been googling this for the last day, and right now all I''ve got is > a headache. There are several articles on this - but they are not making > sense. One is using a field :association_foreign_key that I can''t find > in my documentation.That''s not a field, it''s an option, and it''s clearly explained in the Rails association docs.> I admit it, I''m lost. > > Assume I have a Categories table. To make it simple, give it one field, > a name. > A category can be at the top level (no parents) > A category can be subordinate to another category (it''s child - and > obviously, the first category is the parent. This is a self-referential > many-to-many.Are you *sure* you mean that it''s many-to-many? If that were so, a category could have more than one parent. Is that what you want? Or do you really mean a simple hierarchical tree? Let me know, and perhaps I can be more help. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 June 2010 20:10, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Michael Satterwhite wrote: >> A category can be subordinate to another category (it''s child - and >> obviously, the first category is the parent. This is a self-referential >> many-to-many. > > Are you *sure* you mean that it''s many-to-many? If that were so, a > category could have more than one parent. Is that what you want? Or do > you really mean a simple hierarchical tree?+1 It does sound like you''re quite sure you''re expecting to have many-children and many-parents - almost as if a self-referential has_as_belongs_to_many is what you''re wanting - never tried it myself, but would give it a bash on a test application if that''s what you''re after. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser wrote:> Are you *sure* you mean that it''s many-to-many? If that were so, a > category could have more than one parent. Is that what you want? Or do > you really mean a simple hierarchical tree?That is true. A category can exist in several places in the hierarchy. It may appear (for example) under basic products *AND* under Personal. In that case it has two parents. This is not as common as having multiple children, but both situations are valid. Multiple parents, multiple children.> > Let me know, and perhaps I can be more help.Thanks much. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling wrote:> It does sound like you''re quite sure you''re expecting to have > many-children and many-parents - almost as if a self-referential > has_as_belongs_to_many is what you''re wanting - never tried it myself, > but would give it a bash on a test application if that''s what you''re > after.has_and_belongs_to_many is quite simple when joining 2 different tables (a join table is used). The problem here is that the join is to the *SAME* table. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Satterwhite wrote:> Michael Pavling wrote: > >> It does sound like you''re quite sure you''re expecting to have >> many-children and many-parents - almost as if a self-referential >> has_as_belongs_to_many is what you''re wanting - never tried it myself, >> but would give it a bash on a test application if that''s what you''re >> after. > > has_and_belongs_to_many is quite simple when joining 2 different tables > (a join table is used). The problem here is that the join is to the > *SAME* table.No problem. Read the docs for HABTM. You can have both ends be the same table. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.