Rails通过id找到列为true

时间:2013-08-30 01:42:02

标签: ruby-on-rails postgresql activerecord

我正在尝试按一组ID进行选择,但前提是shown列为真。我试过了: Product.find(@product_ids, conditions: "shown = true")

然而,这给了我错误:Couldn't find all Products with IDs (2, 3) [WHERE (shown = true)] (found 0 results, but was looking for 2)

在这种特殊情况下,所选的两个产品都将shown设置为false。

1 个答案:

答案 0 :(得分:4)

您可以使用where执行此操作。

Product.where("id in (?) and shown = ?", @product_ids, true)

Product.where(id: @product_ids, shown: true)

Product.where("id in (:product_ids) and shown = :shown", { product_ids: @product_ids, shown: true })