title: Using delayed job to process a method in background
body: <pre><code>Rails 5.1
  </code></pre>
  <p>I have the following in my gemfile</p>
  <pre><code>gem 'delayed_job'
  gem 'delayed_job_active_record'
  gem 'daemons'
  </code></pre>
  <p>I installed/updated the gems</p>
  <p>Then I did:</p>
  <pre><code>rails generate delayed_job:active_record
  </code></pre>
  <p>This generated two files in bin called <code>delayed_jobs.rb</code> and created the migration files.</p>
  <p>I added the following to <code>config/application.rb</code>:</p>
  <pre><code>config.active_job.queue_adapter = :delayed_job
  </code></pre>
  <p>I have a controller, with an action, that takes an already uploaded csv file, parses it, and populates the DB with the data. I want that method, or specifically, part of that method to be processed in the background.</p>
  <p>The method is:</p>
  <pre><code>def process_parsed_spreadsheet
    temp_file_path = params[:temp_file_path]
    @spreadsheet   = helpers.open_worksheet(temp_file_path)
    number_of_rows = @spreadsheet.count - 1
  
    helpers.process_spreadsheet(number_of_rows, @spreadsheet, params[:followed_id])
    helpers.remove_uploaded_file(temp_file_path)
  
    redirect_to root_path, notice: t('fw_exports.file_successfully_processed')
  end
  </code></pre>
  <p>I am lost on how to use delayed_jobs to process this method in the background. I read up about having a model, and then having one of the methods in that model process in the background, but this is different.</p>
  <h1>Solution (based on the answer below):</h1>
  <p>I added the following:</p>
  <p>app/jobs/parse_and_process_spreadsheet_job.rb</p>
  <pre><code>class ParseAndProcessSpreadsheetJob &lt; ApplicationJob
    queue_as :default
  
    def perform(temp_file_path, followed_id)
      @spreadsheet = helpers.open_worksheet(temp_file_path)
      number_of_rows = @spreadsheet.count - 1
      helpers.process_spreadsheet(number_of_rows, @spreadsheet, followed_id)
      helpers.remove_uploaded_file(temp_file_path)
      redirect_to root_path, notice: t('fw_exports.file_successfully_processed')
    end
  end
  </code></pre>
  <p>And my app/controllers/fw_exports_controller.rb:</p>
  <pre><code>def process_parsed_spreadsheet
    ParseAndProcessSpreadsheetJob.perform(params[:temp_file_path], params[:followed_id])
  end
  </code></pre>