下面是在对应于我的表类的一个Rails应用程序Ruby的功能:“日期不能为空”,但它不是空白(ActiveRecord的验证)
def self.import(file)
line = 1
CSV.foreach(file.path, headers: true) do |row|
reading_hash = row.to_hash
logger.debug("line #{line}")
logger.debug(reading_hash)
Reading.create!(reading_hash)
line = line + 1
end # end CSV.foreach
end
我看到这个值调试器:
reading_hash = Hash (5 elements)
'date' => 9/19/2015
'name' => Vayeilech
'reading' => Hosea 14:2-10; Micah 7:18-20; Joel 2:15-27 | Shabbat Shuva
'assigned_to' => Spencer Ross (20th bar mitzvah anniversary) Gene Achter (deferred, please find another day for him)
'comments' => Shabbat Shuvah
我还打印散列的记录:
{"date"=>"9/19/2015", "name"=>"Vayeilech", "reading"=>"Hosea 14:2-10; Micah 7:18-20; Joel 2:15-27 | Shabbat Shuva", "assigned_to"=>"Spencer Ross (20th bar mitzvah anniversary) Gene Achter (deferred, please find another day for him)", "comments"=>"Shabbat Shuvah"}
但是,如果我继续为create!
,我会得到:
Validation failed: Date can't be blank
这发生在第三个记录上,前两个工作正常。
也许我也应该举验证:
validates :date, presence: true
是的,我应该可以说服Google床单。谢谢。 – bmargulies
如果你不能说服Google表格 – Zahid
你可以在before_validation回调中设置你的日期格式为什么错误'空白'而我发牢骚'无效日期格式'。 – bmargulies