我有以下代码:
require 'data_mapper'
class Dataset
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
property :year, String, :required => true
end
DataMapper.setup(:default, "sqlite://#{Dir.pwd}/database.db")
DataMapper.finalize.auto_upgrade!
Dataset.all(:year => nil).each do |dataset|
puts dataset.inspect
puts
end
我想找到year
属性为空的所有行。但是当我运行这个脚本时,它给出了空输出。当我像这样运行它时:
Dataset.all(:year => "2005").each do |dataset|
puts dataset.inspect
puts
end
我得到正确的输出:
#<Dataset @id=178 @name="Supernatural" @year="2005">
在 sqlite shell 上运行这个查询会给我 7 行空year
列:
sqlite> SELECT COUNT(`name`) FROM datasets WHERE `year` IS NULL;
COUNT(`name`)
-------------
7
我究竟做错了什么?