# Multiple HABTM relationships with self?

**URL:** <https://www.ruby-forum.com/t/multiple-habtm-relationships-with-self/52892>\
**Category:** Rails\
**Created:** [January 20, 2006, 12:26am UTC](https://www.ruby-forum.com/t/multiple-habtm-relationships-with-self/52892 "2006-01-20T00:26:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 20, 2006, 12:26am UTC](https://www.ruby-forum.com/t/multiple-habtm-relationships-with-self/52892/1 "2006-01-20T00:26:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 20, 2006, 12:36am UTC](https://www.ruby-forum.com/t/multiple-habtm-relationships-with-self/52892/2 "2006-01-20T00:36:31Z")

</div>

Ok - I answered my first question. HABTM works to relate a table to  
itself just fine.

Now how to do it again? (And again!)

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 20, 2006, 2:03am UTC](https://www.ruby-forum.com/t/multiple-habtm-relationships-with-self/52892/3 "2006-01-20T02:03:49Z")

</div>

2nd Q A’d 2. qed

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 20, 2006, 2:58am UTC](https://www.ruby-forum.com/t/multiple-habtm-relationships-with-self/52892/4 "2006-01-20T02:58:02Z")

</div>

maybe this will help?

[http://wiki.rubyonrails.com/rails/pages/HowToUseManyToManyAgainstASingleTable](http://wiki.rubyonrails.com/rails/pages/HowToUseManyToManyAgainstASingleTable)

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 20, 2006, 5:35am UTC](https://www.ruby-forum.com/t/multiple-habtm-relationships-with-self/52892/5 "2006-01-20T05:35:45Z")

</div>

Peter M. wrote:

> maybe this will help?
> 
> [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowToUseManyToManyAgainstASingleTable)

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 –

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 20, 2006, 12:02pm UTC](https://www.ruby-forum.com/t/multiple-habtm-relationships-with-self/52892/6 "2006-01-20T12:02:49Z")

</div>

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 🙂
