After reading this question I have this rake task:
desc "Import sales"
task :import_sales => :environment do
sales = File.open(Rails.root.join("public", "system", "xmls", "3", "original", "first.csv"),"r").map do |line|
isbn, date, quantity, value, channel = line.split("\,")
Importsale.new(:isbn => isbn, :date => date, :quantity => quantity, :value => value, :channel => channel)
end
Importsale.import sales
end
and some csv data like this:
9780953893201,30/5/10,4,25.6,UK Home
9780953893201,25/6/10,3,19.2,UK Home
9780953893201,30/7/10,6,95.94,UK Home
9780953893201,27/8/10,1,15.99,Export
9780953893201,27/8/10,13,207.87,UK Home
9780953893201,27/8/10,1,6.4,UK Home
9780953893201,24/9/10,14,223.86,UK Home
9780953893201,24/9/10,5,32,UK Home
But the rake task is not splitting the CSV file – I’m getting one record imported, with the last channel containing.
UK Home
9780953893201
Is the use of map stopping the csv parsing as lines? If I use each or each_line I get an ‘invalid arguments’ error.
Since no one is writing the answer…