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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -