I'm trying to understand what the difference between these two methods is. Here is the documentation for each:
https://apidock.com/rails/v4.2.7/ActiveModel/Dirty/previous_changes
https://apidock.com/rails/v4.2.7/ActiveModel/Dirty/changes
It appears to me after reading this documentation that previous_changes
is what was changed after the changes are done, meaning in an after_*
filter, while changes
is what will be changed, meaning it's useful in a before_*
filter.
Am I misunderstanding this?
yes you understood it rightly
These are dirty object methods
changes
is used to know the changes that would take place if you try to save an objectprevious_changes
is used to know the changes that reflected by saving an object.But if you try to reload the object both
changes
andprevious_changes
would return empty hash {} as new copy of the record is being fetched from DatabaseEg User(id: 1, name: 'Nimish', age: 24, email: 'test@example.com')