lundi 18 septembre 2017

In a Rails 3 many to many association, what is the most efficient way to query objects based on conditions on their associations?

I have a many-to-many model relation:

class Movie
has_many :movie_genres
has_many :genres, :through => :movie_genres

class Genre
has_many :movie_genres
has_many :movies, :through => :movie_genres

class MovieGenre
belongs_to :movie
belongs_to :genre

I want to query all movies with a certain genre but not associated with another genre. Example: All movies that are Action but not Drama.

What I have done is this:

action_movies = Genre.find_by_name('action').movies
drama_movies  = Genre.find_by_name('drama').movies
action_not_drama_movies = action_movies - drama_movies

Is there a more efficient way of doing this? It should be noted that the query can become more complex like: All movies that are Action but not Drama or All movies that are Romance and Comedy

Aucun commentaire:

Enregistrer un commentaire