# How to model relationship for a model to itself

**URL:** <https://www.ruby-forum.com/t/how-to-model-relationship-for-a-model-to-itself/135450>\
**Category:** Rails\
**Created:** [April 22, 2008, 10:16am UTC](https://www.ruby-forum.com/t/how-to-model-relationship-for-a-model-to-itself/135450 "2008-04-22T10:16:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rockydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@Rockydd](https://www.ruby-forum.com/u/Rockydd)\
**Post date:** [April 22, 2008, 10:16am UTC](https://www.ruby-forum.com/t/how-to-model-relationship-for-a-model-to-itself/135450/1 "2008-04-22T10:16:11Z")

</div>

I have a model named User, and the mysql table is users.  
Every user can have many friends. of course, every friend is a user.  
So, it’s a relationship from user to user.

how to model this?

User has\_and\_belongs\_to\_may :users?

then how to define the table ‘users\_users’ for this relationship.

it need 2 fields named user\_id?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Rockydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@Rockydd](https://www.ruby-forum.com/u/Rockydd)\
**Post date:** [April 22, 2008, 12:12pm UTC](https://www.ruby-forum.com/t/how-to-model-relationship-for-a-model-to-itself/135450/2 "2008-04-22T12:12:12Z")

</div>

Rockydd wrote:

> I have a model named User, and the mysql table is users.  
> Every user can have many friends. of course, every friend is a user.  
> So, it’s a relationship from user to user.
> 
> how to model this?
> 
> User has\_and\_belongs\_to\_may :users?
> 
> then how to define the table ‘users\_users’ for this relationship.

Use the has\_and\_belongs\_to\_many :join\_table option and name it something  
else.

Next, the Friendship concept in a social app might be too important to  
hide  
its join table. “Friends since 2008”, “BFF”, “sworn enemies”, etc. Maybe  
the  
joining table should be a Model of its own. Then User gets…

has\_many :friends  
has\_many :users, :through =\> :friends

…roughly. Can anyone spot what my association is missing?

–  
Phlip

---

<div class="post-metadata">

**Author:** ![Rockydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a9adbd/32.png) [@Rockydd](https://www.ruby-forum.com/u/Rockydd)\
**Post date:** [April 22, 2008, 1:39pm UTC](https://www.ruby-forum.com/t/how-to-model-relationship-for-a-model-to-itself/135450/3 "2008-04-22T13:39:52Z")

</div>

Hi –

On Tue, 22 Apr 2008, Phlip wrote:

> > 
> 
> has\_many :users, :through =\> :friends  
> If you’ve got a Friendship model, though, you’d probably want  
> something like:

has\_many :friendships

and then friends through friendships… except you’ll run into the  
issue of the symmetry (or lack thereof) of the friendships table,  
which of course can’t have two user\_id fields. You can deal with this  
by having a “friend1\_id” and “friend2\_id” (or some such thing) field,  
and a concept of who has befriended whom. There’s some examples of  
this in the comments at:  
[http://blog.hasmanythrough.com/2006/4/21/self-referential-through](http://blog.hasmanythrough.com/2006/4/21/self-referential-through)

I imagine you could also do something with one friendship row and some  
finder SQL, but that will have to await the second cup of coffee 🙂

David

–  
Rails training from David A. Black and Ruby Power and Light:  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
INTRO TO RAILS June 24-27 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
