Firendship representation in rails

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

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 ?

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 http://railsspace.com/book
The book itself would probably be of great interest if you’re going to
build that kind of community :slight_smile:

Gedeon

On Apr 15, 11:16 pm, Gady S. [email protected]

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

http://agilewebdevelopment.com/plugins/has_many_friends

Good starting point is the source their.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs