我已经做了谷歌搜索,发现类似的问题,我的问题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'是未知的,因此您需要对其进行具体说明,克里斯曼德森的答案。 –