Ruby on rails 如何在rails 6的csv文件中导出/导入化身(使用活动存储)
这是我在Product.rb中的导入函数Ruby on rails 如何在rails 6的csv文件中导出/导入化身(使用活动存储),ruby-on-rails,csv,ruby-on-rails-6,rails-activestorage,Ruby On Rails,Csv,Ruby On Rails 6,Rails Activestorage,这是我在Product.rb中的导入函数 def self.import(file) CSV.foreach(file.path, headers: true) do |row| product_hash = row.to_hash product = find_or_create_by!(name: product_hash['name'], price: product_hash['price'], descrip
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
product_hash = row.to_hash
product = find_or_create_by!(name: product_hash['name'], price: product_hash['price'], description: product_hash['description'], avatar: product_hash["avatar"] )
product.tag_list << product_hash['tag_list']
product.update(product_hash)
end
end
def self.to_csv
attributes = %w{id name price tag_list description avatar}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |product|
csv << attributes.map{ |attr| product.send(attr) }
end
end
end
def self.import(文件)
CSV.foreach(file.path,headers:true)do |行|
产品\u散列=行到\u散列
产品=查找或创建用户!(名称:product_hash['name'],价格:product_hash['price'],描述:product_hash['description'],化身:product_hash[“avatar”])
product.tag_列表