我在两个模型superhero和superpower之间有一个rails连接表。现在我有了3种不同的superpower id,我想要所有的superheroes都有所选的superpowers
为了做到这一点,我试图做以下几点:
matches = Superhero.all
matches = matches.joins(:superpowers).where('superpowers.id = ?', 17).where('superpowers.id = ?', 12).where('superpowers.id = ?', 6)但是这给了我一个空对象,即使我有superheroes,它在我的联接表中有所有给定的superpowers
从上面生成的查询是:
SELECT "superheroes".* FROM "superheroes" INNER JOIN "superheroes_superpowers" ON "superheroes_superpowers"."superhero_id" = "superheroes"."id" INNER JOIN "superpowers" ON "superpowers"."id" = "superheroes_superpowers"."superpower_id" WHERE (superpowers.id = 17) AND (superpowers.id = 17) AND (superpowers.id = 12) AND (superpowers.id = 6)因此,奇怪的是,它尝试使用id 17两次检查superpower (但不应该影响结果,我认为),其余的查询似乎是正确的。
发布于 2017-09-10 19:56:33
尝试使用in子句
superpowers_ids = [17,12,6]
matches = Superhero.all
matches = matches.joins(:superpowers).where('superpowers.id in (?)', superpowers_ids)https://stackoverflow.com/questions/46144962
复制相似问题