Topic: Observer oddity
I have lost nearly half my day trying to work this odd problem out.
I am using an observer to trigger an ActionMailer notification
class BmiContactObserver < ActiveRecord::Observer
def after_create(bmi_contact)
BmiContactMailer.deliver_new_bmi_contact_notification(bmi_contact)
end
end
class BmiContactMailer < ActionMailer::Base
def new_bmi_contact_notification(bmi_contact)
setup_email(bmi_contact)
@subject += "To view your BMI again visit"
@body[:url] = "http://#{Diagnosis::Config.site_url}/bmi/#{bmi_contact.body.id}"
end
protected
def setup_email(bmi_contact)
@recipients = "#{bmi_contact.email}"
@from = "#{Diagnosis::Config.admin_email}"
@subject = "[#{Diagnosis::Config.site_url}] "
@sent_on = Time.now
@body[:bmi_contact] = bmi_contact
endend
The problem is I can't access the body association, it comes up blank. However if I call it directly from the controller it works fine (same code).
BmiContactMailer.deliver_new_bmi_contact_notification(@bmi_contact)
I have tried all sorts of different ways to load the body too and all return nil.
Does anyone know if it is possible to access the associations when triggered through an observer?