需要使用 activerecord-import 和 rubyzip gem 将 zip 文件中的 csv 数据导入我的产品模型。
此代码有效(下载 zip 并显示 csv 名称)
desc "Import products data from web"
task import_product: :environment do
url = "https://example.com"
dir = "db/example_zip.zip"
File.open(dir, "wb") do |f|
f.write HTTParty.get(url).body
end
Zip::File.open(dir) do |zip|
zip.each do |entry|
entry.name
end
end
end
在“zip.each 循环”中,我尝试了这个:
items = []
CSV.foreach(entry, headers: true) do |row|
items << Item.new(row.to_h)
end
Item.import(items)
我有以下错误 TypeError: no implicit conversion of Zip::Entry into String
根据本教程:https ://mattboldt.com/importing-massive-data-into-rails/
用这个 csv 刷新我的产品模型数据的最佳方法是什么?我必须将文件读入内存(entry.get_input_stream.read)还是保存文件然后导入?
谢谢你的帮助