how to use observer and call back

  • Add configuration
#config/application.rb
config.active_record.observers = :order_observer
  • Create a Observer Class
    class OrderObserver < ActiveRecord::Observer
      def after_update(order)
        order.update_column('type_state', "UPDATED")
      end
    end 

or we can use the way to register

    #config/application.rb
    config.active_record.observers = :notification_observer

    class NotificationObserver < ActiveRecord::Observer
      observe :account, :balance

      def after_update(order)
        order.update_column('type_state', "UPDATED")
      end
    end 

Any questions on this, please feel free to ask. We’re here to help…

Categories:

Updated: