ruby - Table Join in Rails Active Record -


i have film model

class film < activerecord::base   has_many :film_genres   has_many :genres, through: :film_genres  end 

a genre model

class genre < activerecord::base   has_many :film_genres   has_many :films, through: :film_genres end 

and have filmgenre model

class filmgenre < activerecord::base   belongs_to :film   belongs_to :genre end 

im try list of films in controller under particular genre, cant seem join work.

  def show     @films = ## need join / select here fetch films genre.id of 4   end 

use includes join genres table , can reference in where conditions:

@films = film.includes(:genres).where(genres: {id: 4}) 

alternatively, might easier films genre 4 starting genre model:

@films = genre.find(4).films 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -