延迟作业无法更新参数

时间:2014-07-25 11:10:43

标签: ruby-on-rails delayed-job

我正在尝试在创建交易时发送邮件。邮件的内容包含交易的开始日期,结束日期,年份。当尝试使用传递方法时,邮件包含所有信息。但是,当我尝试使用延迟工作时,我无法获得no.of年。为什么?任何人帮助我?

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)

查看

   <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:  <%=@transaction.num_years %> </p>

日志文件

    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)

1 个答案:

答案 0 :(得分:0)

好吧,你尚未共享Transaction模型,但由于num_years未被序列化为延迟作业记录,我将假设它是因为它是一个实例变量数据库中没有列。 DJ在运行作业时从DB重新加载记录,因此不会初始化实例变量。可以从其他列计算出来吗?如果是这样,请将其设为实例方法而不是变量,以便在DJ从数据库重新加载记录后重新计算。