# Has\_and\_belongs\_to\_many refer to same Model

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-refer-to-same-model/174318>\
**Category:** Rails\
**Created:** [August 25, 2009, 12:24am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-refer-to-same-model/174318 "2009-08-25T00:24:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [August 25, 2009, 12:24am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-refer-to-same-model/174318/1 "2009-08-25T00:24:55Z")

</div>

I have a User model and in I want to have a

has\_and\_belongs\_to\_many :users

Obviously that won’t work since the join table will have the same column  
names (user\_id).

What approach should I take?

thanks

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [August 25, 2009, 2:43am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-refer-to-same-model/174318/2 "2009-08-25T02:43:28Z")

</div>

On Mon, Aug 24, 2009 at 6:24 PM, Allen  
Walker[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a User model and in I want to have a
> 
> has\_and\_belongs\_to\_many :users
> 
> Obviously that won’t work since the join table will have the same column  
> names (user\_id).
> 
> What approach should I take?

You need to give the association a different name, and a few other  
things

class User  
has\_and\_belongs\_to\_many :friends,  
:class =\> “User”,  
:association\_foreign\_key =\>  
“friend\_id”,  
:join\_table =\>  
“friends\_users”  
end

You then need to make a join table named “friends\_users” with no  
primary key and user\_id and friend\_id columns.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [August 25, 2009, 10:26pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-refer-to-same-model/174318/3 "2009-08-25T22:26:19Z")

</div>

Thanks. one thing I noticed is that it’s “:class\_name” not just “:class”  
(2.3.3)

Rick Denatale wrote:

> You need to give the association a different name, and a few other  
> things
> 
> class User  
> has\_and\_belongs\_to\_many :friends,  
> :class =\> “User”,  
> :association\_foreign\_key =\>  
> “friend\_id”,  
> :join\_table =\>  
> “friends\_users”  
> end
> 
> You then need to make a join table named “friends\_users” with no  
> primary key and user\_id and friend\_id columns.
