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 method
path' for "/tmp/RackMultipart20150928-8812-sgwsm5":String`
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