1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-06 08:38:01 +00:00

Add Inspect Previous Changes To ActiveRecord Object as a rails til

This commit is contained in:
jbranchaud
2020-08-27 20:28:47 -05:00
parent 613fc5da06
commit 04ab027f24
2 changed files with 35 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
# 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?`:
```ruby
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?`:
```ruby
book.title = "The Fifth Season"
book.save
book.title_previously_changed? #=> true
book.previous_changes
#=> { "title" => ["Original Title", "The Fifth Season"] }
```
[source](https://api.rubyonrails.org/classes/ActiveModel/Dirty.html)