我正在尝试通过CSV导入产品列表,有一个远程url的图像列。我正在使用activerecord import gem,当我使用下面的设置时,我没有得到任何错误或日志,但镜像没有被创建。
require 'open-uri'
file = open(row['image'])
product = Product.new(
name: row['name'],
sku: row['sku'],
brand_id: row['brand']
)
product.image.attach(io: file, filename: row['sku']+ '.jpg', content_type: 'image/jpg')
products << product
end
importing = Product.import products, recursive: true我是不是遗漏了什么?或者有更好的方法来处理这件事。
我能够使用常规的csv解析来导入图像。仍然不确定,为什么activerecord导入不起作用。我想我会在gem的github上问这个问题。
csv_text = resp.body
csv = CSV.parse(csv_text, :headers => true, :encoding => 'utf-8')
csv.each do |row|
file = open(row['image'])
t = Product.new
t.name = row['name']
t.image.attach(io: file, filename: row['name']+ '.jpg', content_type: 'image/jpg')
t.save
puts "#{t.name} saved"
end发布于 2020-01-08 03:31:34
然后给出这些提示作为答案:
你分享的那部分代码(假设其他部分的行为符合预期)看起来没问题,似乎它在做它应该做的事情,但由于你的图像并没有被创建……(删除猜测部分)
编辑:在您的更新揭示了一些上下文之后,我猜测第一个版本(带有ActiveRecord导入)不起作用,因为导入gem不会调用回调,并且/或者可能会跳过ActiveStorage触发文件附件上传所需的某些内容。
https://stackoverflow.com/questions/59620226
复制相似问题