mirror of
https://github.com/jbranchaud/til
synced 2026-01-02 22:58:01 +00:00
926 B
926 B
Inspect Previous Changes To ActiveRecord Object
If you modify an ActiveRecord object, before saving it, you can inspect changes
with methods like changed? and <attr>_changed?:
book.title = "The Fifth Season"
book.changed? #=> true
book.title_changed? #=> true
book.publication_year_changed? #=> false
book.changes
#=> { "title" => ["Original Title", "The Fifth Season"] }
After saving an object, it will no longer be in a dirty state and these methods will have no changes to return.
If you have a reference to the saved ActiveRecord object, you can look at the
previous changes with methods like previous_changes and
<attr>_previously_changed?:
book.title = "The Fifth Season"
book.save
book.title_previously_changed? #=> true
book.previous_changes
#=> { "title" => ["Original Title", "The Fifth Season"] }