3

Userstableとtable をリンクする 2 つの関係 m:n タイプがありますVideos。次の行コマンドで作成します。

rails generate migration users_comments_videos
rails generate migration users_views_videos

ファイル user.rb と videos.rb に、それぞれ次の手順を追加しました。

has_and_belongs_to_many :users
has_and_belongs_to_many :videos

これらの 2 つの指示は、作成した両方の関係に対して有効ですか?

4

1 に答える 1

2

別の関連付け名を選択してから、モデルを指定します。

user.rb

class User
  has_many :comments
  has_many :views
  has_many :comment_videos, :through => :comments, :source => 'Video'
  has_many :view_videos, :through => :views, :source => 'Video'
end

video.rb

class Video
  has_many :comments
  has_many :views
  has_many :comment_users, :through => :comments, :source => 'User'
  has_many :view_users, :through => :views, :source => 'User'
end
于 2012-06-02T19:37:59.637 に答える