Forum: Ruby on Rails Multiple HABTM relationships with self ???

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Da P. (Guest)
on 2006-01-20 01:26
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!
Da P. (Guest)
on 2006-01-20 01:36
Ok - I answered my first question.  HABTM works to relate a table to
itself just fine.

Now how to do it again?  (And again!)
Da P. (Guest)
on 2006-01-20 03:03
2nd Q A'd 2. qed
Peter M. (Guest)
on 2006-01-20 03:58
(Received via mailing list)
Da P. (Guest)
on 2006-01-20 06:35
Peter M. wrote:
> maybe this will help?
>
> http://wiki.rubyonrails.com/rails/pages/HowToUseMa...

Geez, 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 --
Alex Y. (Guest)
on 2006-01-20 13:02
(Received via mailing list)
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 :-)
This topic is locked and can not be replied to.