这是类中与我的表对应的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
答案 0 :(得分:1)
您可以将csv日期格式化为以下格式之一吗?这将使它工作。任何这些格式都会插入您想要的日期:
2015/09/19
2015/9/19
19/09/2015
答案 1 :(得分:0)
在日期周围加上引号。我想这会解决它。