我正在尝试实现一个社交网络风格的友情模型,我没有太多的运气试图找出那里可用的插件。如果我自己做的话,我想我会更好地学习Rails。所以这就是我所拥有的:
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
答案 0 :(得分:7)