I need to have a table related to itself via a join table. Will HABTM support this? That is: class Recipe < ActiveRecord::Base has_and_belongs_to_many :recipes, :join_table => "recipes_recipes" end More importantly -- how does RoR support MULTIPLE self-joins? I.e., to relate the table to itself via multiple join tables. SFAIK, HABTM won''t support multiple self-joins because the method names would get confused. Could I create additional controllers for the class, and then use the :class_name option to specify those controllers? SOrt of like so: class Recipe < ActiveRecord::Base set_primary_key "R_ID" has_and_belongs_to_many :recipes, :join_table => "recipes_recipes", :foreign_key => "R_ID", :association_foreign_key => "R_ID2" has_and_belongs_to_many :recipes2, :join_table => "recipes_recipes2", :foreign_key => "R_ID", :association_foreign_key => "R_ID2" has_and_belongs_to_many :recipes3, :join_table => "recipes_recipes3", :foreign_key => "R_ID", :association_foreign_key => "R_ID2" end Thanks! -- Posted via http://www.ruby-forum.com/.
Ok - I answered my first question. HABTM works to relate a table to itself just fine. Now how to do it again? (And again!) -- Posted via http://www.ruby-forum.com/.
2nd Q A''d 2. qed -- Posted via http://www.ruby-forum.com/.
Peter Michaux
2006-Jan-20 01:18 UTC
[Rails] Re: Multiple HABTM relationships with self ???
maybe this will help? http://wiki.rubyonrails.com/rails/pages/HowToUseManyToManyAgainstASingleTable
dr plutes
2006-Jan-20 03:59 UTC
[Rails] Re: Re: Multiple HABTM relationships with self ???
Peter Michaux wrote:> maybe this will help? > > http://wiki.rubyonrails.com/rails/pages/HowToUseManyToManyAgainstASingleTableGeez, I had seen that article before, and was looking for it today, but for some reason I couldn''t locate it when I needed it (doh!). A bit late, as I already figured a way, but Thanks just same -- I think I ended up doing it somewhat differently - this article doesn''t display everything so I can''t be sure. Since I''ve already completed this I didn''t replicate their approach so I''m not exactly sure what''s missing in this howto. Anyway, all I did was create a new model for each relationship R1, R2, ..., R5 against the same table R and then within each of these new models used set_table_name R and set_primary_id. Then it was a simple matter to create a HABTM for each of these new self-referential joins in the model for R. Slick! Same effect though, partially connected node graph. BTW are the SQL statements behind HABTM reasonably optimized? I''m using about 5 of these self-referential queries simultaneously against a moderately large database and it is taking about 5-10 seconds to return. I''m wondering what is the next step for either optimizing this query or somehow caching results dynamically or perhaps pre-caching results in advance (hopefully without losing generality). Thanks again -- -- Posted via http://www.ruby-forum.com/.
Alex Young
2006-Jan-20 10:23 UTC
[Rails] Re: Re: Multiple HABTM relationships with self ???
dr plutes wrote:> BTW are the SQL statements behind HABTM reasonably optimized? I''m using > about 5 of these self-referential queries simultaneously against a > moderately large database and it is taking about 5-10 seconds to return. > I''m wondering what is the next step for either optimizing this query or > somehow caching results dynamically or perhaps pre-caching results in > advance (hopefully without losing generality).I doubt they''d be overly optimised beyond the basics - it''d probably be worth checking what turns up in the development log to see if you can see anything obviously wrong. And, as always, check your indices :-) -- Alex