Advanced friendship model and queries in Rails 3 -
i have friendship model, , every new friendship, create 2 new records:
- user1 , friend1
- friend1 , user1
i can retrieve standard staffs like: friends, friendships, pending_friends... situation becomes complicated when try common friends, friends of friends...
for common friendships, use like:
has_many :common_friendships, :class_name => 'friendship', :conditions=>'friendships.user_id = #{self.id}' 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 can use full query finder_sql
like:
select distinct * friendships fl1 inner join friendships fl2 on fl1.friend_id = fl2.user_id fl1.user_id = 1 , fl2.friend_id = 2
how can in elegant way in rails 3?
check out answer on question:
how implement friendship model in rails 3 social networking application?
Comments
Post a Comment