Callback for changed ActiveRecord attributes?

后端 未结 6 1977
一生所求
一生所求 2020-12-14 03:07

I am aware of ActiveRecord::Dirty and the related methods, but I don\'t see a means by which I can subscribe to an attribute changed event. Something like:

         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 03:38

    For Rails 3:

    class MyModel < ActiveRecord::Base
      after_update :my_listener, :if => :my_attribute_changed?
    
      def my_listener
        puts "Attribute 'my_attribute' has changed"
      end
    end
    

    Commented in: https://stackoverflow.com/a/1709956/316700

    I don't find official documentation about this.

提交回复
热议问题