'RoR: Export CSV process asynchronously using a worker?

I'm trying to use a Sidekiq worker to handle CSV downloads in the background. Ordinarily a user clicks a button and the csv downloads right away. I'd like to process the csv asynchronously in a worker:

In my User model:

def self.generate_csv
  CSV.generate(headers: true) do |csv|
    csv << [I18n.t('models.user.csv_header.email'),
           blah blah
  all.each do |user|
    csv << [user.email_i18n,
           blah blah
  end
end

In my User Controller:

    format.csv do
      #what I normally do
      #response.headers['Content-Type'] = 'text/csv'
      #response.headers['Content-Disposition'] = "attachment; filename=#{@models.camelize}.csv"
      #send_data @collection.generate_csv, filename: "#{filename}.csv"
---------------------------------------------------------------
    
      CsvDownloadWorker.perform_async(@collection, filename)
    end

In my CsvDownloadWorker:

 class CsvDownloadWorker
    include Sidekiq::Worker
  
    sidekiq_options queue: :default, retry: 0

    def perform(collection, filename)
      ????
    end
end

I have no idea where to go from here and have tried various convoluted approaches with no success. I would really appreciate any guidance big or small. ty



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source