How to Use Observer and Call Back

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

or we can use the way to register

1
2
3
4
5
6
7
8
9
10
#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…

Comments

Comments