我试图实施一个社会网络风格的友谊的模式和我没有更多的运气,试图找出的插件可在那里。我想我会了解轨道更好的如果我做我自己。因此,这里的我有什么:

class User < ActiveRecord::Base
  has_many :invitee_friendships ,
           :foreign_key => :friend_id,
           :class_name => 'Friendship'

  has_many :inviter_friends,
            :through => :invitee_friendships

  has_many :inviter_friendships ,
           :foreign_key => :user_id,
           :class_name => 'Friendship'

  has_many :invited_friends,
            :through => :inviter_friendships

end

class Friendship < ActiveRecord::Base
  belongs_to :user
  //I think something needs to come here, i dont know what
end

irb 当我试试这个:

friend1  = Friend.create(:name => 'Jack')
friend2  = Friend.create(:name => 'John')
bff = Friendship.create(:user_id =>1, :friend_id => 2)
f1.invited_friends

我得到一个错误:

ActiveRecord::HasManyThroughSourceAssociationNotFoundError:
Could not find the source
association(s) :invited_friend or
:invited_friends in model Friendship. 
Try 'has_many :invited_friends,
:through => :invited_friendships,
:source => <name>'.  Is it one of
:user?

感觉在巴黎的景观主题的友谊系统:

  • 用户可以邀请其他用户成为朋友。
  • 用户应邀成为朋友表示由 invited_friends.
  • 用户是谁邀请你成为朋友表示由 inviter_friends.
  • 你总的朋友列表,表示由 invited_friends + inviter_friends.

架构

table Friendship
      t.integer :user_id
      t.integer :friend_id
      t.boolean :invite_accepted
      t.timestamps

table User
    t.string :name
    t.string :description
有帮助吗?

解决方案

我很惊讶没有人已经指出,最近的瑞恩贝茨 视频 在主题:)

希望这可以帮助!.

摘自瑞恩'...需要一个自引用关于用户的模型来定义的朋友/追随者

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top