簡體   English   中英

Ruby on Rails中的朋友的朋友

[英]Friends of friends in Ruby on Rails

我有一個自我推薦的協會Friendship 用戶通過友誼有很多朋友。 我想獲得朋友的朋友清單。 什么是最有效的方法? 我正在使用ActiveRecord。 謝謝!

class User < ActiveRecord::Base

  has_many :friendships
  has_many :friends, through => :friendships, :class_name => "User"

  def friends_of_friends
    User.joins(:friendships).where(:user_id => friendships.pluck(:friend_id))
  end

end

友情模型將類似於

class Friendship < ActiveRecord::Base

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

end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM