> 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/check-if-activerecord-update-fails.md).

# Check If ActiveRecord Update Fails

There are two ways to update an `ActiveRecord` instance (not to mention [`assign_attributes`](https://api.rubyonrails.org/classes/ActiveModel/AttributeAssignment.html), [`update_attribute`](https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_attribute), etc.).

You can call [`update`](https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update) and [`update!`](https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update-21). If the updates would make the record invalid based on the model's validations, then the update will fail.

You can tell if `update` failed because it will return `false` (instead of `true`).

```ruby
unless book.update(book_params)
  log_book_update_failed(id: book.id)
end
```

The `update!` version will raise an `ActiveRecord::ActiveRecordError` exception if the update fails.

```ruby
begin
  book.update!(book_params)
rescue ActiveRecord::ActiveRecordError
  log_book_update_failed(id: book.id)
end
```


---

# 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/check-if-activerecord-update-fails.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.
