# HABTM association with own class?

**URL:** <https://www.ruby-forum.com/t/habtm-association-with-own-class/113965>\
**Category:** Rails\
**Created:** [September 6, 2007, 6:54pm UTC](https://www.ruby-forum.com/t/habtm-association-with-own-class/113965 "2007-09-06T18:54:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 6, 2007, 6:54pm UTC](https://www.ruby-forum.com/t/habtm-association-with-own-class/113965/1 "2007-09-06T18:54:47Z")

</div>

If I have a class, Asset, which has and belongs to many associated Asset  
objects, how do I do this?

has\_and\_belongs\_to\_many :associated\_assets, :class =\> “Asset”,  
:foreign\_key =\> ???, :association\_foreign\_key =\> ???

Right now, my table has a :first\_asset\_id and a :second\_asset\_id, but I  
don’t see how the objects themselves are supposed to distinguish one  
from the other.

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 6, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/habtm-association-with-own-class/113965/2 "2007-09-06T21:18:19Z")

</div>

Surely someone here has done this?

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 6, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/habtm-association-with-own-class/113965/3 "2007-09-06T21:26:40Z")

</div>

Nevermind, I guess a join model is the only way to go.

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 7, 2007, 7:04am UTC](https://www.ruby-forum.com/t/habtm-association-with-own-class/113965/4 "2007-09-07T07:04:55Z")

</div>

Dave, with a web site with that name I am sure Josh knows a lot more  
about this than I do, but in the case you describe it looks like you  
just want a simple self-join. In which case I think what you want is:

def Asset \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :related\_assets,  
:class\_name =\> “Article”,  
:join\_table =\> “related\_assets”,  
:association\_foreign\_key =\> “related\_asset\_id”,  
:foreign\_key =\> “asset\_id”  
end

And then a table called related\_assets with the columns  
related\_asset\_id  
and asset\_id

Cheers, --Kip

On Sep 7, 6:43 am, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 7, 2007, 12:43am UTC](https://www.ruby-forum.com/t/habtm-association-with-own-class/113965/5 "2007-09-07T00:43:39Z")

</div>

Dave S. wrote:

> If I have a class, Asset, which has and belongs to many associated Asset  
> objects, how do I do this?
> 
> has\_and\_belongs\_to\_many :associated\_assets, :class =\> “Asset”,  
> :foreign\_key =\> ???, :association\_foreign\_key =\> ???
> 
> Right now, my table has a :first\_asset\_id and a :second\_asset\_id, but I  
> don’t see how the objects themselves are supposed to distinguish one  
> from the other.

You should be able to do this by using TWO associations and specifying a  
slew of options. You need to override pretty much all the basic  
defaults. Create a join table called ‘assets\_assets’ for the below  
example.

Asset:  
has\_and\_belongs\_to\_many :first\_assets, :class\_name =\> “Asset”,  
:join\_table =\> “assets\_assets”, :foreign\_key =\> “second\_asset\_id”,  
:association\_foreign\_key =\> “first\_asset\_id”  
has\_and\_belongs\_to\_many :second\_assets, :class\_name =\> “Asset”,  
:join\_table =\> “assets\_assets”, :foreign\_key =\> “first\_asset\_id”,  
:association\_foreign\_key =\> “second\_asset\_id”

Change those association names as you want. Note, this is written in the  
email so it’s untested. It’s straightforward though so I’d expect it to  
work.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
