# How to define relationship for user-to-user friendship

**URL:** <https://www.ruby-forum.com/t/how-to-define-relationship-for-user-to-user-friendship/88132>\
**Category:** Rails\
**Created:** [March 8, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/how-to-define-relationship-for-user-to-user-friendship/88132 "2007-03-08T12:43:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jesse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jesse](https://www.ruby-forum.com/u/jesse)\
**Post date:** [March 8, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/how-to-define-relationship-for-user-to-user-friendship/88132/1 "2007-03-08T12:43:43Z")

</div>

Was wondering how I would define this relationship in my User model if:  
A friendship relationship consists of two users.

---

<div class="post-metadata">

**Author:** ![jesse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jesse](https://www.ruby-forum.com/u/jesse)\
**Post date:** [March 8, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/how-to-define-relationship-for-user-to-user-friendship/88132/2 "2007-03-08T13:07:42Z")

</div>

Jesse wrote:

> Was wondering how I would define this relationship in my User model if:  
> A friendship relationship consists of two users.

class CreateUsers \< ActiveRecord::Migration  
def self.up  
create\_table :users do |t|  
t.column :name, :string, :limit =\> 80, :null =\> false  
t.column :friend\_id, :integer  
end  
end

def self.down  
drop\_table :users  
end  
end

class User \< ActiveRecord::Base

belongs\_to :friend,  
:class\_name =\> “User”,  
:foreign\_key =\> “friend\_id”

end

[276]script/console  
Loading development environment.

> > john = User.create(:id =\> 1, :name =\> “John”)  
> > jane = User.create(:id =\> 2, :name =\> “Jane”)  
> > john.friend = jane  
> > jane.friend = john  
> > john.friend\_id  
> > =\> 2
> 
> > jane.friend\_id  
> > =\> 1

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![jesse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jesse](https://www.ruby-forum.com/u/jesse)\
**Post date:** [March 8, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/how-to-define-relationship-for-user-to-user-friendship/88132/3 "2007-03-08T20:16:24Z")

</div>

Jesse wrote:

> Was wondering how I would define this relationship in my User model if:  
> A friendship relationship consists of two users.

You’ll want to set up a self-referential many-to-many relationship. For  
that you’ll need two models, User and Friendship (the join model). You  
can base it on the example I show in this article:

[http://blog.hasmanythrough.com/2006/4/21/self-referential-through](http://blog.hasmanythrough.com/2006/4/21/self-referential-through)

Map Node to User, Edge to Friendship. Change the association names as  
makes sense for your models. You may also want to read this article:

[http://blog.hasmanythrough.com/2006/8/19/magic-join-model-creation](http://blog.hasmanythrough.com/2006/8/19/magic-join-model-creation)

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