I currently have many emails which are tied to various states within a model. IE, if the state goes from "pending" to "confirmed" an email gets generated.
I'm now rethinking whether the email generation should be in the model or in the controller? I've been updating my mailers so that they perform asymmetrically and this has meant replacing my previous code which passed database-backed objects as parameters into my mailer and instead pass record identifiers, so they can be looked up later.
The problem with doing this from within the model is that the record doesn't actually save until all the steps in the method have been called. So it breaks my mailers if they depend on any of the steps, etc.
Should I move all of this out of the model altogether? Or is there some other way to make sure it saves before performing the mailing?
event :confirm do
transitions :to => :confirmed, :from => :pending, :on_transition => :do_confirm
end
def do_confirm(finished_at = Time.now)
se开发者_如何学Pythonlf.confirmed_at = finished_at
UserMailer.confirmed_booking_request(self).deliver # old way, doesn't matter if record is saved or not
UserMailer.confirmed_booking_request(self.id).deliver #doesn't work since the record hasn't been saved and confirmed_at is nil
end
or should I do email generation within my controllers
if @confirmation.save
@confirmation.booking.reject!
UserMailer.confirmed_booking_request(@confirmation.id).deliver #previously did this all from the above method
redirect_to space_path(@confirmation.booking.space)
end
How about putting your email generation in an ActiveRecord::Oserver instead?
精彩评论