callback, but not very related to model
we can use observer
class AuditObserver < ActiveRecord::Observer
observe :account, :balance
def after_update(record)
AuditTrail.new(record, "UPDATED")
end
end
observe :account, :balance
def after_update(record)
AuditTrail.new(record, "UPDATED")
end
end
Storing Observers in Rails
If you’re using Active Record within Rails, observer classes are usually stored in app/models with the naming convention of app/models/audit_observer.rb.
Configuration
In order to activate an observer, list it in the
config.active_record.observers
configuration setting in yourconfig/application.rb
file.
config.active_record.observers = :comment_observer, :signup_observer
Observers will not be invoked unless you define these in your application configuration.
沒有留言:
張貼留言