Submitted by swarut on Fri, 10/26/2012 - 16:32
In rails, the normal mail sending task via Mailer is asynchronus. Thus, in the mailer method, we pass the id of the model instead of the model itself because the life time of the model may be ended before the mail sending task is started.
However, for the background job, the mail sending task should not be done asynchronusly. We run the background job, and mail sending is also a background job. This mean we can run both of them together. We can also have the model entity available at running time.
The below codes show the different implementation of mail sending method in asynchronus and synchronus manner respectively.
-
def mentoring_survey(pending_survey_id)
@pending_survey = Survey.find(pending_survey_id)
@user = @pending_survey.user
sendgrid_category "Mentoring Survey Notification"subject = t('survey_mailer.mentoring_survey.subject')
mail(to: @user.email, subject: subject) do |format|
format.text
format.html { render layout: 'mailer' }
endend
def mentoring_survey_reminder(pending_survey)
@pending_survey = pending_survey@user = pending_survey.user
sendgrid_category "Mentoring Survey Reminder"subject = t('survey_mailer.mentoring_survey_reminder.subject')
mail(to: @user.email, subject: subject) do |format|
format.text
format.html { render layout: 'mailer' }
endend