# Self-referential many-many joins with :through

**URL:** <https://www.ruby-forum.com/t/self-referential-many-many-joins-with-through/93298>\
**Category:** Rails\
**Created:** [March 30, 2006, 12:12am UTC](https://www.ruby-forum.com/t/self-referential-many-many-joins-with-through/93298 "2006-03-30T00:12:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![henster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a183cd/32.png) [@henster](https://www.ruby-forum.com/u/henster)\
**Post date:** [March 30, 2006, 12:12am UTC](https://www.ruby-forum.com/t/self-referential-many-many-joins-with-through/93298/1 "2006-03-30T00:12:44Z")

</div>

I thought I had this nailed but… now I’m seeing spots…

I’ve included my models below, feel free to ignore them. I’m really  
just after an example that works. I couldnt find one on the wiki…  
which is fair enough considering Ricks patch:  
[http://dev.rubyonrails.org/changeset/4022](http://dev.rubyonrails.org/changeset/4022) that fixed them only went  
through 5 days ago…

Cheers  
-henster

* * *

class Topic \< ActiveRecord::Base

has\_many :item\_collection, :class\_name =\> “Grouping”, :foreign\_key  
=\> “item\_id”  
has\_many :group\_collection, :class\_name =\> “Grouping”,  
:foreign\_key =\> “group\_id”

has\_many :groups, :through =\> :group\_colletion  
has\_many :items, :through =\> :item\_collection

end

* * *

* * *

class Grouping \< ActiveRecord::Base

belongs\_to :item,  
:class\_name =\> “Topic”,  
:foreign\_key =\> “item\_id”

belongs\_to :group,  
:class\_name =\> “Topic”,  
:foreign\_key =\> “group\_id”  
end

* * *

---

<div class="post-metadata">

**Author:** ![henster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a183cd/32.png) [@henster](https://www.ruby-forum.com/u/henster)\
**Post date:** [March 30, 2006, 1:13am UTC](https://www.ruby-forum.com/t/self-referential-many-many-joins-with-through/93298/2 "2006-03-30T01:13:16Z")

</div>

I’m assuming that the spelling difference for group\_collection was only  
when  
type the email. Could you tell us what isn’t working? Rick’s change  
added  
some very useful error messages that should help you out.

Anyway, I think your has\_many :through relationships might be backwards.

has\_many :groups, :through =\> :group\_collection

This is going to connect to the groupings table and specify the  
group\_id,  
since you specified that as the foreign key in the has\_many  
:group\_collection relationship. It will then try to join back to the  
topics  
table using group\_id again, because you name the through relationship  
“groups”. It tries to find a relationship on the join table that is the  
singular or plural of the name of the through relationship, unless you  
specify the :source option.

Do you really need a join model in this case? If your join model  
doesn’t do  
anything besides hold the keys to the topics table, then a regular HABTM  
may  
suit you better.

You may want to read through [http://dev.rubyonrails.org/ticket/4289](http://dev.rubyonrails.org/ticket/4289) to  
see  
some background for changeset 4022.

-Lee

On 29 Mar 2006 21:09:46 -0000, Henry T. \<

---

<div class="post-metadata">

**Author:** ![henster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a183cd/32.png) [@henster](https://www.ruby-forum.com/u/henster)\
**Post date:** [March 30, 2006, 2:22am UTC](https://www.ruby-forum.com/t/self-referential-many-many-joins-with-through/93298/3 "2006-03-30T02:22:55Z")

</div>

Hey Lee,

Thanks for the reply. It was nice to soak up some backstory from the  
ticket. I see you had a bit of grief with it. Just playing around with  
the new features and seeing whats possible. I can see there is no need  
for :through in this example, but is there a good reason to change it?  
Easier to expand later if needs be. It looks like performance should be  
the same… I see these kind of things are being said elsewhere so I’ll  
keep an eye out.

Cheers  
-h
