# Model relationship problem

**URL:** <https://www.ruby-forum.com/t/model-relationship-problem/120484>\
**Category:** Rails\
**Created:** [November 17, 2007, 12:35pm UTC](https://www.ruby-forum.com/t/model-relationship-problem/120484 "2007-11-17T12:35:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piotr\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@Piotr\_N](https://www.ruby-forum.com/u/Piotr_N)\
**Post date:** [November 17, 2007, 12:35pm UTC](https://www.ruby-forum.com/t/model-relationship-problem/120484/1 "2007-11-17T12:35:52Z")

</div>

Hi,

how do i do a relation for my users table ?  
The problem is following:

im building community site, where are users. every user can have a  
friend which is user too.  
so i created migration which adds to my users table new column user\_id,  
and i also addes has\_many relationship to User model.

so i want that both of them have reference to each other, they are  
friends right ? im not sure if i have to user belongs\_to too…

1. is this ok ?
2. i dont want to use syntax like: user.users, but rather  
user.friends…  
how do i do this ? in rails im forced to create user\_id column which is  
accesible through users property in my user model, this is convenction  
over cofiguration and i dont like it in this particular situation.

thanks in advance

regards

---

<div class="post-metadata">

**Author:** ![Piotr\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@Piotr\_N](https://www.ruby-forum.com/u/Piotr_N)\
**Post date:** [November 17, 2007, 4:43pm UTC](https://www.ruby-forum.com/t/model-relationship-problem/120484/2 "2007-11-17T16:43:35Z")

</div>

Piotr N. wrote:

> how do i do a relation for my users table ?  
> The problem is following:
> 
> im building community site, where are users. every user can have a  
> friend which is user too.  
> so i created migration which adds to my users table new column user\_id,  
> and i also addes has\_many relationship to User model.
> 
> so i want that both of them have reference to each other, they are  
> friends right ? im not sure if i have to user belongs\_to too…
> 
> 1. is this ok ?
> 2. i dont want to use syntax like: user.users, but rather  
> user.friends…

You’ll need a self-referential has\_many :through setup. I cover how to  
do it here:  
[http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through](http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through)

> how do i do this ? in rails im forced to create user\_id column which is  
> accesible through users property in my user model, this is convenction  
> over cofiguration and i dont like it in this particular situation.

There are very few conventions in Rails that are forced on you. Keep  
reading up and you’ll see how to configure things to work differently  
when you need to.

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