class Film < ActiveRecord::Base
has_many :film_casts
has_many :derectors, :through => :film_casts, :source => :cast, :conditions => ['film_casts.type_id = 0']
has_many :performers, :through => :film_casts, :source => :cast, :conditions => ['film_casts.type_id = 1']
end
class Cast < ActiveRecord::Base
end
class FilmCast < ActiveRecord::Base
# type_id 0 => derector, 1 => performer
belongs_to :film
belongs_to :cast
end
irb(main):001:0> films = Film.includes(:performers, :derectors).limit(5)
Film Load (0.3ms) SELECT `films`.* FROM `films` LIMIT 2
FilmCast Load (0.3ms) SELECT `film_casts`.* FROM `film_casts` WHERE `film_casts`.`film_id` IN (1, 2) AND (film_casts.type_id = 1)
Cast Load (0.4ms) SELECT `casts`.* FROM `casts` WHERE `casts`.`id` IN (2, 3, 4, 5, 6, 7, 9, 10, 11, 12)
質問は、なぜそれだけが含まれているの:performers
ですか? 両方を同じテーブルに含めるにはどうすればよいですか?