0

協会の仕組みがよくわかりません。

私は3つのモデルを持っています:

  1. 映画(:about, :title, :url, :actors, :uploader)
  2. 役者(:出生, :名前)
  3. 関係(:actor_id、:film_id)

リレーションシップは、映画と俳優の間の関連付けであるため、「どの俳優がどの映画で演じるか」です。先生はhttp://railscasts.com/episodes/47-two-many-to-manyを使えば簡単にできると教えてくれましたが、使い方がわかりません。

4

1 に答える 1

3

次のようなものが欲しいと思います:

class Movie < ActiveRecord::Base
  has_many :actors, :through => :relationships
end

class Relationship < ActiveRecord::Base
  belongs_to :movie
  belongs_to :actor
end

class Actor < ActiveRecord::Base
  has_many :movies, :through => :relationships
end

http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

または、リレーションシップ クラスを明示的に定義する必要がないと思われる場合は、次のように単純に使用できますhas_and_belongs_to_many

class Movie < ActiveRecord::Base
  has_and_belongs_to_many :actors
end

class Actor < ActiveRecord::Base
  has_and_belongs_to_many :movies
end
于 2013-04-03T11:16:34.093 に答える