Using ActiveRecord, is there a way to get the old values of a record during after_update

后端 未结 10 1229
耶瑟儿~
耶瑟儿~ 2021-01-30 08:20

Setup using a simple example: I\'ve got 1 table (Totals) that holds the sum of the amount column of each record in a second table (

10条回答
  •  忘了有多久
    2021-01-30 08:31

    From Rails 5.1, the behavior of attribute_was inside of after callbacks have change. attribute_was will return the value after the save is done and return the current value in an after_save or after_update callback.

    attribute_before_last_save is invoked two ways to get the previous value of a field in the after_save and after_update callbacks right now:

    Option #1

    attribute_before_last_save('yourfield')
    

    Option #2

    *_before_last_save
    

提交回复
热议问题