I've built a custom "products" engine. I'd like to import a long list of products via a CSV file, but I frankly have no idea how. This seems fairly straightforward in plain vanilla Rails, but it just isn't working at all inside my custom engine. I've built the following rake task to handle the import:
require 'csv'
namespace :refinery do
namespace :products do
# call this task by running: rake refinery:products:import_csv
desc "Import products via CSV"
task :import_csv => :environment do
CSV.foreach('db/product.refinery_products.csv') do |row|
puts "Counting headers in CSV file..."
product_id = row[0]
model_number = row[1]
category = row[2]
subcategory = row[3]
description = row[4]
meta_heading = row[5]
logo = row[6]
lo_res_image = row[7]
hi_res_image = row[8]
main_description = row[9]
feature_indications = row[10]
certification = row[11]
puts "Adding products to database..."
Refinery::Products.create(
product_id: product_id,
model_number: model_number,
category: category,
subcategory: subcategory,
description: description,
meta_heading: meta_heading,
logo: logo,
lo_res_image: lo_res_image,
hi_res_image: hi_res_image,
main_description: main_description,
feature_indications: feature_indications,
certification: certification
)
puts "Finished"
end
end
end
end