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

后端 未结 10 1045
耶瑟儿~
耶瑟儿~ 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
    
    0 讨论(0)
  • 2021-01-30 08:33

    Ditto what everyone is saying about transactions.

    That said...

    ActiveRecord as of Rails 2.1 keeps track of the attribute values of an object. So if you have an attribute total, you will have a total_changed? method and a total_was method that returns the old value.

    There's no need to add anything to your model to keep track of this anymore.

    Update: Here is the documentation for ActiveModel::Dirty as requested.

    0 讨论(0)
  • 2021-01-30 08:45

    If you want to get value of particular field after update you can use field_before_last_save method.

    Example:
    
    u = User.last
    u.update(name: "abc")
    
    u.name_before_last_save will give you old value (before update value)
    
    0 讨论(0)
  • 2021-01-30 08:46

    Idea 1: Wrap the update in a database transaction, so that if the update fails your Totals table isn't changed: ActiveRecord Transactions docs

    Idea 2: Stash the old value in @old_total during the before_update.

    0 讨论(0)
  • 2021-01-30 08:47

    Add this to your model:

    def amount=(new_value)
        @old_amount = read_attribute(:amount)
        write_attribute(:amount,new_value)
    end
    

    Then use @old_amount in your after_update code.

    0 讨论(0)
  • 2021-01-30 08:48

    Firstly, you should be doing this in a transaction to ensure that your data gets written together.

    To answer your question, you could just set a member variable to the old value in the before_update, which you can then access in the after_update, however this isn't a very elegant solution.

    0 讨论(0)
提交回复
热议问题