協会の仕組みがよくわかりません。
私は3つのモデルを持っています:
- 映画(:about, :title, :url, :actors, :uploader)
- 役者(:出生, :名前)
- 関係(:actor_id、:film_id)
リレーションシップは、映画と俳優の間の関連付けであるため、「どの俳優がどの映画で演じるか」です。先生はhttp://railscasts.com/episodes/47-two-many-to-manyを使えば簡単にできると教えてくれましたが、使い方がわかりません。
協会の仕組みがよくわかりません。
私は3つのモデルを持っています:
リレーションシップは、映画と俳優の間の関連付けであるため、「どの俳優がどの映画で演じるか」です。先生はhttp://railscasts.com/episodes/47-two-many-to-manyを使えば簡単にできると教えてくれましたが、使い方がわかりません。
次のようなものが欲しいと思います:
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