我已經做了谷歌搜索,發現類似的問題,我的問題here,但是,我仍然失去了什麼解決方案應該是。我的情況如下:Rails 4:model.where.not不返回零記錄
我有一個名爲Book
的數據庫表,它有category_id
(表Category
的外鍵)和author
(varchar 255)。我需要通過category_id
查詢此表,但不包括某個作者的姓名。例如:由作者李四得到所有CATEGORY_ID = 10的書籍,但不包括書籍:
books = Book.where(category_id: 10).where.not(author: 'John Doe')
或
books = Book.where(category_id: 10).where("author != ?", "John Doe")
這兩個查詢工作對我來說,除了對那些記錄的作者是nil
。我還需要讓這些查詢返回具有nil
作者的記錄。
你嘗試過嗎? books = Book.where(category_id:10).where(「author <>?OR author =?」,「John Doe」,nil) – power
'NULL'是未知的,因此您需要對其進行具體說明,克里斯曼德森的答案。 –