Search code examples
ruby-on-railsdelayed-jobar-mailer

Lost values after switching email sending from AR_Mailer to DelayedJob


I've been using AR_Mailer for about 6 months without ever running into problems. I recently added DelayedJob for some administrative background jobs. Since DelayedJob also handles emails very well (thanks to DelayedMailer gem) I completely removed AR_Mailer from my application.

Everything works perfectly except this email. The password that is generated automatically is now lost.

#app/models/notifier.rb 
def activation_instructions(user)
 from          default_email
 @bcc          = BACK_UP
 @subject      = "Activation instructions" 
 recipients    user.email
 sent_on       Time.now
 body          :root_url => root_url, :user => user
end

#app/views/notifier/activation_instructions.erb
Thanks for signing up.

Your password is <%=@user.password-%>. For security reasons please change this on your first connection.

[....]

Any idea on why this bug occurs? Thanks!

Configuration: Rails 2.3.2 & DelayedJob 2.0.4


Solution

  • I found out where the problem was. I looked in the database at the entry created in the delayed_jobs table:

      --- !ruby/struct:Delayed::PerformableMethod
      object: LOAD;Notifier
      method: :deliver_activation_instructions!
      args:
      - LOAD;User;589
    

    The user parameter is reloaded from the database by delayed_job before sending the email. In that case, the password is lost because it's not stored in the database.

    So I've updated the code in order to pass explicitly the password:

    #app/models/notifier.rb 
    def activation_instructions(user, password)
     from          default_email
     @bcc          = BACK_UP
     @subject      = "Activation instructions" 
     recipients    user.email
     sent_on       Time.now
     body          :root_url => root_url, :user => user, :password => password
    end
    
    #app/views/notifier/activation_instructions.erb
    Thanks for signing up.
    
    Your password is <%=@password-%>. For security reasons please change this on your first connection.
    
    [....]
    

    Hope this helps other too!