0

こんにちは、ストーリー、いいね、ユーザーのレールにいくつかのオブジェクトがあります

ソーシャル ネットワーク (facebook、google+ など) として「いいね」システムを作成したいのですが、「いいね」はユーザーではなくストーリーに表示されます。

そこで、ストーリーとユーザーの間に Like オブジェクト [関係] を作成しました

私のオブジェクトは以下のようになります:

class Story < ActiveRecord::Base
  has_many :likes , dependent: :destroy
  has_many :users , through: :likes, source: :users
  belongs_to :user
end

次のようなオブジェクト:

class Like < ActiveRecord::Base
 attr_accessible :story_id, :user_id
 belongs_to :users
 belongs_to :story
end

ユーザーではなくストーリーに「いいね」があるため、ユーザーに「いいね」があると言うのは誤りです。

私はしたくない:

user.likes

そしてユーザーモデル:

class User < ActiveRecord::Base
 attr_accessible :avatar, :email

 has_many :stories
 #What am i missing here?
end

問題は、ストーリーが好きなユーザーをどのように獲得できるかです。私は次のようなことをしたい:

s = Story.find(2) s.likes.users

ここで何が欠けていますか?

ありがとう。

4

1 に答える 1

0

ユーザーで

class User < ActiveRecord::Base
  attr_accessible :avatar, :email
  has_many :likes , dependent: :destroy
  has_many :stories,through: :likes
end
于 2012-08-20T10:38:23.687 に答える