4

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

1 に答える 1

7

このトピックに関する最近のライアン・ベイツのスクリーンキャストを誰も指摘していないことに驚いています:)

お役に立てれば!。

ライアンからの抜粋'...友達/フォロワーを定義するには、ユーザーモデルに自己参照の関連付けが必要です'

于 2009-06-23T20:47:00.717 に答える