Run a callback only if an attribute has changed in Rails

前端 未结 3 579
孤城傲影
孤城傲影 2020-12-22 22:18

I have the following association in my app:

# Page 
belongs_to :status

I want to run a callback anytime the status_id of a

3条回答
  •  余生分开走
    2020-12-22 22:43

    Try this

    after_validation :do_something, if: ->(obj){ obj.status_id.present? and obj.status_id_changed? } 
    
    def do_something
     # your code
    end
    

    Reference - http://apidock.com/rails/ActiveRecord/Dirty

提交回复
热议问题