下面是与我的表对应的rails应用程序中的Ruby函数:
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
发布于 2015-09-19 00:53:08
您能将您的csv日期格式化为下列格式之一吗?这会让它发挥作用的。这些格式中的任何一个都将插入您想要的日期:
2015/09/19
2015/9/19
19/09/2015
发布于 2015-09-19 00:31:44
在日期前后加上引号。我想这会解决问题的。
https://stackoverflow.com/questions/32662806
复制相似问题