tilo/smarter_csv

NoMethodError: undefined method `close' for nil:NilClass

antarr opened this issue · 2 comments

I"m getting this error, NoMethodError: undefined method close for nil:NilClass when attempting to process a file. It happens with both v1 and v2.

bill_import_worker.rb

class BillImportWorker
  include Sidekiq::Worker
  sidekiq_options retry: 5, queue: :legiscan_imports

  def perform(file_path)
    options = {
      chunk_size: 1000,
      header_transformations: [
        :none,
          :keys_as_strings,
          { key_mapping: key_mapping }
      ]
    }
    chuncks = SmarterCSV.process(file_path, options)
    chuncks.each do |chunk|
      LegiscanModel::Bill.upsert_all(chunk)
    end
  end

  def key_mapping
    {
      'bill_id' => 'bill_id',
      'session_id' => 'session_id',
      'bill_number' => 'middle_name',
      'session' => 'session_id',
      'status_desc' => nil,
      'status_date' => 'status_date',
      'title' => 'title',
      'description' => 'description',
      'committee_id' => 'committee_id',
      'last_action_date' => 'last_action_date',
      'last_action' => 'last_action',
      'url' => 'url',
      'state_link' => 'state_link'
    }
  end
end
tilo commented

@antarr

  • where do you get the file_path from? is it a filehandle from File.open?
    It looks like you're passing nil in, instead of a filehandle on line 5.

  • which version of smarter_csv did you try exactly?

This should avoid this:

f.close if f.respond_to?(:close)

I don't recall what solution I used to fix this. But the issue was related to a change in Ruby 3. I'll comment later if I find it.