1

I have this rake task where I'm trying to import a CSV file into my db. Some reason it doesn't just finish, but instead throws an error on the very last line. I tried stopping it earlier but it still throws the same error. I also made sure there weren't any other lines past the last valid value. To import I'm using the activerecord-import gem. See my code below:

desc 'add customer to my db'
  task :add_my_db => :environment do
  puts "adding to my internal database.. hold the line"
  add
end
require 'csv'
require 'pp'

def add
  rows_to_insert = []
  CSV.foreach("customers.csv", headers: true) do |row|
    pp row
    rows_to_insert << row
  end
  Customer.import(rows_to_insert)
end

The error it throws is: ArgumentError: Invalid arguments!

Where did I go wrong?

ToddT
  • 3,084
  • 4
  • 39
  • 83
  • What error does it throw? – whodini9 Aug 16 '17 at 20:41
  • updated the question with the error.. – ToddT Aug 16 '17 at 21:06
  • 1
    CSV code looks fine. `ArgumentError: Invalid arguments!` [comes from activerecord-import](https://github.com/zdennis/activerecord-import/search?utf8=%E2%9C%93&q=Invalid+arguments%21). A stacktrace would be nice. – sshaw Aug 17 '17 at 02:39
  • Have a look at this answer: https://stackoverflow.com/questions/45074980/rspec-and-rails-4-update-skip-callback/45077040#45077040, and debug your code to see exactly what's the problem. – Mugur 'Bud' Chirica Aug 17 '17 at 15:32

1 Answers1

0

While working with CSV for bulk uploads, it's better to catch the errors using rescue exceptions, and store the error items in a temp object, if you need to keep track of failed lines. And then use next in the rescue to continue to the next line. It would go something like this:

def add
  rows_to_insert = []
  error_rows = []
  CSV.foreach("customers.csv", headers: true) do |row|
    begin
      pp row
      rows_to_insert << row
    rescue => Exception
      error_rows << row
      next
    end
  end
  Customer.import(rows_to_insert)
  # export/show your error_rows here
end