0

I have friendship model, and for every new friendship, I create two new records:

  • User1 and Friend1
  • Friend1 and User1

I can retrieve all standard staffs like: friends, friendships, pending_friends... The situation becomes complicated when I try to get common friends, friends of friends...

For now to get common friendships, I use something like:

has_many :common_friendships, :class_name => 'Friendship', :conditions=>'friendships.user_id = #{self.id}' do
  def common_with(friend)
    joins("inner join friendships fl2 on friendships.friend_id = fl2.user_id").where("fl2.friend_id = #{friend.id}")
  end
end 

Also I can use full query with finder_sql like:

select distinct *
from friendships fl1
inner join friendships fl2 on fl1.friend_id = fl2.user_id
where fl1.user_id = 1  and fl2.friend_id = 2

How can I do that in an elegant way in Rails 3?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Alex
  • 191
  • 4
  • Are you using single model for storing freinds and users information??? – Ashish Mar 04 '11 at 10:36
  • no of course. There is a User and Friendship model – Alex Mar 04 '11 at 13:36
  • 1
    Possible duplicate of [How to Implement a Friendship Model in Rails 3 for a Social Networking Application?](http://stackoverflow.com/questions/5612736/how-to-implement-a-friendship-model-in-rails-3-for-a-social-networking-applicati) – Tunaki Jun 25 '16 at 17:35

1 Answers1

0

Check out the answer on this question:

How to Implement a Friendship Model in Rails 3 for a Social Networking Application?

Community
  • 1
  • 1
Tilo
  • 33,354
  • 5
  • 79
  • 106