将邮件发送器包含在有状态模型中或将其保留在控制器中



我目前有许多与模型内的各种状态相关联的电子邮件。在IE中,如果状态从"待定"变为"确认",则生成一封电子邮件。

我现在正在重新考虑电子邮件生成是否应该在模型或在控制器?我一直在更新我的邮件,使他们执行不对称,这意味着取代我以前的代码传递数据库支持的对象作为参数到我的邮件,而不是传递记录标识符,所以他们可以以后查找。

在模型中这样做的问题是,记录实际上不会保存,直到方法中的所有步骤都被调用。因此,如果我的邮件依赖于任何步骤,它就会破坏我的邮件,等等。

我应该把所有这些都移出模型吗?或者是否有其他方法可以确保在执行邮件之前进行保存?

  event :confirm do
    transitions :to => :confirmed, :from => :pending, :on_transition => :do_confirm
  end
  def do_confirm(finished_at = Time.now)
      self.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

或者我是否应该在控制器中生成电子邮件

  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

将您的电子邮件生成放在ActiveRecord::Oserver中如何代替?

最新更新