Greg Edwards
2006-Feb-16 00:00 UTC
[Rails] HABTM -VS- belongs_to/has_many, for self-referential joins
Ok, David says on page 241 that sometimes a many-to-many relation with attributes are better implemented as an actual model instead of using HABTM. Well, I''ve got that situation and I can''t figure it out. All of the examples in the book have HABTM examples between 2 different tables, but I want to have a HABTM relation on 1 table with itself. (e.g., if I have a table Things, then I would like one "Thing" to "helpout" another Thing) ---------------------- Table 1: "Thing" Column: Name ---------------------- Table2: "Helps" Column: thing_id (e.g. "this thing.") Column: helps_id (. "helps that thing") ---------------------- It seems like I might want to use a HABTM, but I can''t figure out the settings. OR (and I think this is what I actually need / want to do), I need a separate model to represent Helps which has some belongs_to / has_many relationships in it (I think this is better because then I could do some interesting complex reporting on the "Helps" table, such as "who helps the most number of other things?"). Any ideas how to set this up in the Models? And, what would be the right way to say: thing1.helps << thing2 (i.e. we need to document the fact that thing1 helps thing2)? Thanks, -Greg Greg Edwards CTO, Eyetools Inc. greg.railslist@eyetools.com <mailto:gedwards1@eyetools.com> (916) 792 4538 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060216/2236fdaf/attachment-0001.html
Dylan Stamat
2006-Feb-16 02:07 UTC
[Rails] HABTM -VS- belongs_to/has_many, for self-referential joins
I think Chad tackles this exact scenerio in the Recipes book that''s out in beta (available in PDF) right now. Don''t have it on my computer right now, but I think you need to do something like: Thing habtm :helps, :class_name => "Thing" :join_table => "helps_things" ... etc... where the "helps_things" is your Join table (so you''d have one "things" table, and one "helps_things" table). I think you have to maybe specify :foreign_key => "thing_id" and there is another declaration maybe of :assoc_foreign_key => "thing" maybe... I stared at this chapter in the PDF for a loooooooong time, but probably forgot something. You can probably figure it out from the above, and looking at the HABTM options. =Dylan On 2/15/06, Greg Edwards <gedwards1@eyetools.com> wrote:> > Ok, David says on page 241 that sometimes a many-to-many relation with > attributes are better implemented as an actual model instead of using HABTM. > Well, I''ve got that situation and I can''t figure it out? > > > > All of the examples in the book have HABTM examples between 2 different > tables, but I want to have a HABTM relation on 1 table with itself. (e.g., > if I have a table Things, then I would like one "Thing" to "helpout" another > Thing) > > > > ---------------------- > > Table 1: "Thing" > > Column: Name > > ---------------------- > > Table2: "Helps" > > Column: thing_id (e.g. "this thing?") > > Column: helps_id (? "helps that thing") > > ---------------------- > > > > It seems like I might want to use a HABTM, but I can''t figure out the > settings? > > OR (and I think this is what I actually need / want to do), I need a > separate model to represent Helps which has some belongs_to / has_many > relationships in it (I think this is better because then I could do some > interesting complex reporting on the "Helps" table, such as "who helps the > most number of other things?"). > > > > Any ideas how to set this up in the Models? And, what would be the right > way to say: > > thing1.helps << thing2 (i.e. we need to document the fact that thing1 > helps thing2)? > > > > Thanks, > > -Greg > > > > > > Greg Edwards > > CTO, Eyetools Inc. > > greg.railslist@eyetools.com <gedwards1@eyetools.com> > > (916) 792 4538 > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060216/51a39534/attachment-0001.html
Possibly Parallel Threads
- Strange axis labels?
- recommended setup of Mongrel_cluster + SSL for multiple apps?
- Autotest doesn''t work on WinXP?
- Modifying "Find" to always add a condition?
- Is there a way to abstract the updating of other parts of the page after an Ajax request without putting using the standard view file?