rails activerecord,friend relation + inverse_friend关系如何获得相互关系?代码包括在内

Rub*_*tic 1 activerecord ruby-on-rails mutual-friendship

试图找到相互关系,在朋友关系中,已经有朋友和inverse_friends.但是如何将它们结合起来以获得共同的朋友呢? 似乎无法弄明白我尝试了几个选项并在网上搜索了很长时间,只是看不到它

  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
Run Code Online (Sandbox Code Playgroud)

怎么得到一个

has_many :mutual_friends ?
Run Code Online (Sandbox Code Playgroud)

Rob*_*ert 5

你需要两种模式才能找到共同的朋友吗?

你不能这样做吗?

@mutualfriends = @user1.friends & @user2.friends
Run Code Online (Sandbox Code Playgroud)