> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/inspect-previous-changes-to-activerecord-object.md).

# 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)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/inspect-previous-changes-to-activerecord-object.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
