根据这个example , 以下方法:
require "csv"
def import_vault_data(filename)
fn = "#{RAILS_ROOT}/public/data/#{filename}"
CSV.foreach(fn, :headers => true) do |row|
House.create!(row.to_hash)
end
end
产生此错误:
undefined method `to_hash' for #<Array:0x104cc07b8>
有什么遗漏的线索吗?
我正在使用 rails 2.3.9
最佳答案
答案可能有点晚,但您需要:
CSV.foreach(file.path, headers: true) do |row|
因为你不能调用 to_hash 除非你在散列中有标题
关于ruby-on-rails - 将 .csv 文件保存到无法识别 to_hash 方法的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12756670/