Ruby on rails 表Join-in-Rails活动记录
我有一个电影模特Ruby on rails 表Join-in-Rails活动记录,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有一个电影模特 class Film < ActiveRecord::Base has_many :film_genres has_many :genres, through: :film_genres end 使用includes加入流派表,然后可以在中引用它,其中条件: @films = Film.includes(:genres).where(genres: {id: 4}) 或者,从类型开始,可能更容易获得类型4的所有电影: @films = Genre.find(
class Film < ActiveRecord::Base
has_many :film_genres
has_many :genres, through: :film_genres
end
使用
includes
加入流派表,然后可以在中引用它,其中
条件:
@films = Film.includes(:genres).where(genres: {id: 4})
或者,从类型开始,可能更容易获得类型4的所有电影:
@films = Genre.find(4).films
你犯了什么错误?谢谢。这两种方法都有效,第二种方法读起来更好。
def show
@films = ## need a join / select here to fetch all films with Genre.id of 4
end
@films = Film.includes(:genres).where(genres: {id: 4})
@films = Genre.find(4).films