# Firendship representation in rails

**URL:** <https://www.ruby-forum.com/t/firendship-representation-in-rails/134855>\
**Category:** Rails\
**Created:** [April 15, 2008, 6:09pm UTC](https://www.ruby-forum.com/t/firendship-representation-in-rails/134855 "2008-04-15T18:09:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [April 15, 2008, 6:09pm UTC](https://www.ruby-forum.com/t/firendship-representation-in-rails/134855/1 "2008-04-15T18:09:07Z")

</div>

Hi  
I want to represent a social graph in my facebooker application.  
I have a class user and I want to connect each user to his friends using  
many-to-many. I am planning to support other networks in the future so I  
have to keep that in mind

what’s the best way to implement this ?

My idea is to create a friendship table that holds user\_id, user\_id and  
create a has\_many :through relationship.  
I’m not sure how to do it when the two models are the same.

class User \< ActiveRecord::Base  
has\_many :friendships  
has\_many :users, :through =\> :friendships  
end  
class Friendship\< ActiveRecord::Base  
belongs\_to :User  
belongs\_to :User #?  
end

create\_table “users” do |t|  
t.column “name”, :string  
end

create\_table “friendships” do |t|  
t.column “user\_id”, :integer  
t.column “user\_id”, :integer #?  
end

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [April 15, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/firendship-representation-in-rails/134855/2 "2008-04-15T18:16:53Z")

</div>

another question.  
since it’s a mutual friendship some entries might be :  
user\_id, user\_id :

preson1, person2  
person2, person1

is this duplication a problem ? how do i handle it ?

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [April 16, 2008, 12:37pm UTC](https://www.ruby-forum.com/t/firendship-representation-in-rails/134855/3 "2008-04-16T12:37:58Z")

</div>

Hi Gady,

The RailsSpace application code could help you a lot on this matter as  
it has the exact same structure.

You can get it there [Railsspace.com is for sale | HugeDomains](http://railsspace.com/book)  
The book itself would probably be of great interest if you’re going to  
build that kind of community 🙂

Gedeon

On Apr 15, 11:16 pm, Gady S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [April 16, 2008, 12:47pm UTC](https://www.ruby-forum.com/t/firendship-representation-in-rails/134855/4 "2008-04-16T12:47:30Z")

</div>

Gedeon wrote:

> Gedeon
> 
> > is this duplication a problem ? how do i handle it ?

Duplication is always a problem.

You might simply force the person1\_id being less (or equal for narcissic  
people) than person2\_id.

Lionel

---

<div class="post-metadata">

**Author:** ![gadys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gadys](https://www.ruby-forum.com/u/gadys)\
**Post date:** [April 16, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/firendship-representation-in-rails/134855/5 "2008-04-16T23:03:25Z")

</div>

[http://agilewebdevelopment.com/plugins/has\_many\_friends](http://agilewebdevelopment.com/plugins/has_many_friends)

Good starting point is the source their.
