私はソーシャルネットワーキングスタイルの友情モデルを実装しようとしていますが、そこにあるプラグインを見つけようとしてもあまり運がありませんでした。自分でやれば、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