database - find attribute in a rails model with another has_many relationship -
i don't know how bring question, going explain it.
i have next models: movie, genre , theater. movies , theaters have many-to-many relationship. movies , genres have same relationship.
in rails:
- theaters
has_many
movies - movies
belongs_to
theaters - movies
has_many
genres - genres
belongs_to
movies
my problem:
with theater = theater.all.first
(for example) can first theater , theater.movies
, movies theater has. how can movies specific genre? must nice way it, din't find any.
first of describe "in rails", not many-to-many relation, one-to-many. has_and_belongs_to_many
or has_many :through
relations. see activerecord associations
what want, should easy. if have genre
available, a_genre.movies
work. otherwise you'll need start movie
, construct query:
movie.joins(:genres).where({genres: { name: ['comedy', 'drama']}}) movie.joins(:genres, :theaters).where({genres: { name: ['comedy', 'drama']}}). where({theaters: { city: 'ny' }})
Comments
Post a Comment