How to model a mutual friendship in Rails

♀尐吖头ヾ 提交于 2019-12-21 20:01:32

问题


I know this question has been asked before on Stack Overflow, but the answers aren't doing it for me in ways I can explain. My general approach was inspired by this tutorial.

What I'm trying to do, is create a really simple model for friending users that creates an equivalent friendship on both ends with a single record.

At the db level, I just have a 'friendships' table that just has a user_id, a friend_id, and an is_pending boolean column.

In user.rb I've defined the relationship as:

has_many :friendships
has_many :friends, through: :friendships

In friendship.rb, I've defined the relationship as:

belongs_to :user
belongs_to :friend, :class_name => 'User'

If I add a friendship, I can access as follows:

> a = User.first
> b = User.last
> Friendship.new(a.id, b.id)
> a.friends
=> #<User b>

That's perfect, but what I want is to also be able to go in the other direction like so:

> b.friends

Unfortunately, with the relationship defined as it is, I get an empty collection. The SQL that runs shows that it's searching for user_id = b.id. How do I specify that it should also search for friend_id = b.id?


回答1:


Maybe this:

friendship.rb
belongs_to :friend_one, :foreign_key => :user_id
belongs_to :friend_two, :foreign_key => :friendship_id

and

user.rb

has_many :friendship_ones, :class_name => 'Friendship', :foreign_key => :friendship_id
has_many :friend_ones, through: :friendship_ones
has_many :friendship_twos, :class_name => 'Friendship', :foreign_key => :user_id
has_many :friend_twos, through: :friendship_twos


def friends
  friend_ones + friend_twos
end

You get two queries to find the friends, but it is a simple data model and you you do just call @user.friends to find the instances.

It would be amenable to eager loading, if you load the two friend_ones and friend_twos associations.




回答2:


This article shows how to set up reciprocal relationships: Bi-directional relationships in Rails

It shows how to use after_create and after_destroy to insert additional relationships that model the reciprocal relationship. In that way, you'd have double the records in your join table, but you'd have the flexibility of using a.friends and b.friends and seeing that both include each other correctly.

Making it work with your model:

class Person < ActiveRecord::Base
  has_many :friendships, :dependent => :destroy
  has_many :friends, :through => :friendships, :source => :person
end

class Friendship < ActiveRecord::Base
  belongs_to :person, :foreign_key => :friend_id
  after_create do |p|
    if !Friendship.find(:first, :conditions => { :friend_id => p.person_id })
      Friendship.create!(:person_id => p.friend_id, :friend_id => p.person_id)
    end
  end
  after_update do |p|
    reciprocal = Friendship.find(:first, :conditions => { :friend_id => p.person_id })
    reciprocal.is_pending = self.is_pending unless reciprocal.nil?
  end
  after_destroy do |p|
    reciprocal = Friendship.find(:first, :conditions => { :friend_id => p.person_id })
    reciprocal.destroy unless reciprocal.nil?
  end
end

I've used this approach successfully on a few projects, and the convenience is fantastic!



来源:https://stackoverflow.com/questions/37244283/how-to-model-a-mutual-friendship-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!