4
我試圖實現一個社交網絡風格的友誼模型,我沒有太多的運氣,試圖找出那裏的插件。我想我會更好地學習Rails,如果我自己做。因此,這裏是我有:如何在ActiveRecord中創建自反自聯接關係?
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?
Expanation友誼制度:
- 用戶可以邀請其他用戶成爲朋友。
- 您邀請成爲朋友的用戶由
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
你會得到什麼錯誤? – 2009-06-23 18:50:22
剛剛添加了錯誤 – udit 2009-06-23 18:56:33