Search code examples
ruby-on-railsdelayed-job

Delayed job could not update the parameter


I am trying to send mail when a transaction is created .Content of the mail contains the start date ,end date, no.of years of the transaction. When try this using deliver method mail contains all information. But when i try this using delayed job i could not get the no.of years .Why? any one help me?

def create
  @transaction = Transaction.new(params[:transaction])
  if transaction.start_date.present?
   transaction.save!
   Mailers.delay.transaction_created_notification(@transaction)
end

transaction.save!
Mailers.delay.transaction_created_notification(@transaction)

View

   <h3>The Transaction has been created.</h3>
   <p> Transaction start date: <%= @transaction.start_date %></p>
   <p> Transaction end date: <%= @transaction.end_date %></p>
   <p>No of years:  <%[email protected]_years %> </p>

Log file

    Started POST "transaction" for 127.0.0.1 at 2014-07-25 16:02:10 +0530
       Processing by TransactionController#create as HTML
           Parameters{"utf8"=>"✓","authenticity_token"=>"ziUJghnsfUjuWA5ar17lgZ0QaOz0Psm3L8lN0wXa7tY=", "transaction"=>{ "start_date(1i)"=>"2014", "start_date(2i)"=>"7", "start_date(3i)"=>"1", "num_years"=>"10"}, "commit"=>"Create Transaction"}


   (0.1ms)  BEGIN
   SQL (0.3ms)  INSERT INTO `delayed_jobs` (`attempts`, `created_at`, `failed_at``handler`, `last_error`, `locked_at`, `locked_by`, `priority`, `queue`, `run_at`, `updated_at`) VALUES (0, '2014-07-25 10:32:10', NULL, '--- !ruby/object:Delayed::PerformableMailer\nobject: !ruby/class \'Mailers\'\nmethod_name: :transaction_created_notification\nargs:\n- !ruby/ActiveRecord:Transaction\n attributes:\n id: 435\n school_id: 371\n start_date: 2014-07-01\n end_date: 2024-06-30\n created_at: &1 2014-07-25 10:32:10.446748247 Z\n updated_at: *1\n', NULL, NULL, NULL, 0, NULL, '2014-07-25 10:32:10', '2014-07-25 10:32:10')
    (40.6ms)  COMMIT
    Redirected to http://sat.lvh.me:3000/Transaction/371
    Filter chain halted as #         <Proc:0x0000000bd6d408@/home/kavi/newproject/app/admin/Transaction.rb:40> rendered or redirected
   Completed 302 Found in 108.4ms (ActiveRecord: 68.8ms)

Solution

  • Well, you haven't shared the Transaction model yet, but since num_years is not being serialized into the Delayed Job record, I'm going to assume it's because it's an instance variable that does not have a column in the database. DJ reloads the record from the DB when it runs the job, so an instance variable won't be initialized. Can it be computed from the other columns? If so, make it an instance method instead of a variable, so that it gets recalculated after DJ reloads the record from the DB.