2016-05-07 67 views
0

現在我用這個搜索查找我的數據庫更改搜索查詢軌

@items=Item.where(:category_id => @active_category_id).order(:price) 

對某些類別的項目但最近我添加一列Item表稱爲detail。它可以是0或1,它是integer,或者它可以是空的,因爲我剛剛添加它,並且我已經有一些項目在我的數據庫中。 所以現在我需要兩個搜索:我需要搜索返回的項目detail=1

在哪裏detail不是1 所以我不喜歡這樣寫道:

#for items with detail = 1

@items=Item.where(:category_id => @active_category_id) .where(:detail=> 1).order(:price)

它工作中。

但現在我需要找到與detail != 1

所以我寫

@items=Item.where(:category_id => @active_category_id) .where.not(:detail=> 1).order(:price) 項目和它不工作。我該怎麼辦?

+0

比較哪個您使用的導軌版本是? 'Where.not()'在Rails 4中引入 – TheGeorgeous

+0

@TheGenetic'gem'rails','4.2.5.1'' – user2950593

+0

什麼是生成的輸出和SQL查詢?你可以在問題中更新它嗎? – Pavan

回答

0

NULL值不匹配相等或不等。你必須明確地比較NULL。嘗試Item.where(detail: nil)

如果您需要0 OR NULL你可能需要寫原始SQL:Item.where("detail = 0 OR detail IS NULL")

您還可以考慮回填你的數據庫,以消除NULL值,那麼你可以只是1和0