2013-08-30 20 views
0

我試圖通過一組ID來選擇,但只有在shown列爲真時纔可以。我想: Product.find(@product_ids, conditions: "shown = true")通過ID找到欄位爲真的軌道

不過是給我的錯誤:Couldn't find all Products with IDs (2, 3) [WHERE (shown = true)] (found 0 results, but was looking for 2)

在這種特殊情況下,這兩種選擇的產品有shown設置爲false。

+0

從導遊:「Model.find(array_of_primary_key),將會提高一個ActiveRecord :: RecordNotFound例外,除非匹配的記錄,發現所有的提供的主鍵「。這就是爲什麼你收到這個例外。 –

回答

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 })