mysql - Rails extract values with join associations -
i have rails join table between 2 models superhero
, superpower
. have 3 different superpower id
, want superheroes
have selected superpowers
to i'm trying following:
matches = superhero.all matches = matches.joins(:superpowers).where('superpowers.id = ?', 17).where('superpowers.id = ?', 12).where('superpowers.id = ?', 6)
but gives me empty object though have superheroes
have given superpowers
in join table
the query generated above is:
select "superheroes".* "superheroes" inner join "superheroes_superpowers" on "superheroes_superpowers"."superhero_id" = "superheroes"."id" inner join "superpowers" on "superpowers"."id" = "superheroes_superpowers"."superpower_id" (superpowers.id = 17) , (superpowers.id = 17) , (superpowers.id = 12) , (superpowers.id = 6)
so weirdly tries check superpower
id 17 twice (but shouldn't affect result think) , rest of query seems correct.
try using in clause
superpowers_ids = [17,12,6] matches = superhero.all matches = matches.joins(:superpowers).where('superpowers.id in (?)', superpowers_ids)
Comments
Post a Comment