【发布时间】:2016-07-18 16:17:54
【问题描述】:
我进行了谷歌搜索,发现与我的问题here 类似的问题,但是,我仍然不知道应该是什么解决方案。我的情况如下:
我有一个名为Book 的数据库表,其中有category_id(表Category 的外键)和author(varchar 255)。我需要通过category_id 查询此表,但不包括某些作者的姓名。示例:获取 category_id = 10 的所有书籍,但不包括作者 John Doe 的书籍:
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)
-
NULL未知,因此您需要根据@chrismanderson 的回答具体说明。
标签: ruby-on-rails ruby postgresql