Search code examples
ruby-on-railscsvsidekiq

undefined method `path' with csv import via sidekiq


I am trying to import a large csv via sidekiq. But i am facing problem.

I wrote import method in my controller.

  def import
   Importcsv.perform_async(params[:file])
   redirect_to calendars_path, notice: "Calendar imported."
  end

And Worker code is here.

class Importcsv
  include Sidekiq::Worker
  sidekiq_options queue: "high"

 def perform(file)
   CSV.foreach(file.path, headers: true) do |row|
   calendar = find_by_id(row['id']) || new
     calendar.attributes = row.to_hash
     calendar.save
   end
 end
end

But i got this error undefined methodpath' for "/tmp/RackMultipart20150928-8812-sgwsm5":String`


Solution

  • I solved this issue by passing single row, e.g.

    CSV.foreach(params[:file].path, headers: true) do |row|
      attrs ={name: row[0]} 
      Importcsv.perform_async(attrs)
    end