# HABTM -VS- belongs\_to/has\_many, for self-referential joins

**URL:** <https://www.ruby-forum.com/t/habtm-vs-belongs-to-has-many-for-self-referential-joins/53887>\
**Category:** Rails\
**Created:** [February 16, 2006, 1:00am UTC](https://www.ruby-forum.com/t/habtm-vs-belongs-to-has-many-for-self-referential-joins/53887 "2006-02-16T01:00:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecc23a/32.png) [@Greg\_E](https://www.ruby-forum.com/u/Greg_E)\
**Post date:** [February 16, 2006, 1:00am UTC](https://www.ruby-forum.com/t/habtm-vs-belongs-to-has-many-for-self-referential-joins/53887/1 "2006-02-16T01:00:50Z")

</div>

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 E.

CTO, Eyetools Inc.

removed\_email\_address@domain.invalid [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

(916) 792 4538

---

<div class="post-metadata">

**Author:** ![Greg\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecc23a/32.png) [@Greg\_E](https://www.ruby-forum.com/u/Greg_E)\
**Post date:** [February 16, 2006, 3:08am UTC](https://www.ruby-forum.com/t/habtm-vs-belongs-to-has-many-for-self-referential-joins/53887/2 "2006-02-16T03:08:00Z")

</div>

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
