質問

ソーシャルネットワーキングスタイルの友情モデルを実装しようとしていますが、利用可能なプラグインを見つけようとしてあまり運がありませんでした。 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
役に立ちましたか?

解決

最近のライアン・ベイツのスクリーンキャストトピックについて:)

これが役立つことを願っています!

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top