0

This has probably been asked but I am not even sure how to phrase this suitable for querying.

I wish to call user1.friends (user1 is an instance of the User model) which returns a list of users who all include user1 as one of their friends.

I am not sure where to even start with this one.

What would be my columns for the friends table? Should there even be a friends table?

super_noobling
  • 323
  • 5
  • 11

1 Answers1

1

Maybe you could use this RailsCasts

models/user.rb:

has_many :friendships
has_many :friends, :through => :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user

models/friendship.rb

belongs_to :user
belongs_to :friend, :class_name => "User"
Diego Polido Santana
  • 1,425
  • 11
  • 19