Skip to content
Richard Huang edited this page Aug 15, 2010 · 3 revisions

Please go to http://rails-bestpractices.com/posts/19-use-observer

Before:


class Project < ActiveRecord::Base
  after_create :send_create_notifications

  private

  def send_create_notifications
    self.members.do |member|
      ProjectMailer.deliver_notification(self, member)
    end
  end

end

After:


class Project < ActiveRecord::Base
  # nothing here
end

class ProjectNotificationObserver < ActiveRecord::Observer
  observe Project

  def after_create(project)
    project.members.each do |member|
      ProjectMailer.deliver_notice(project, member)
    end
  end

end