# Rails: ActiveRecord::StaleObjectError: Attempted to update a stale object: Order

**Code:** `ActiveRecord::StaleObjectError (optimistic locking)` · **Category:** Ruby / Rails · **Seen on:** Ruby

Two users/processes updated the same record; the second save failed the `lock_version` check.

## Meaning

This is optimistic locking working as designed (a `lock_version` column exists). Reload the record and reapply the change, or show a conflict message to the user. Remove the `lock_version` column if you don't want it, or retry in background jobs with `record.reload`.

## Common causes

- Concurrent edits to the same row
- `lock_version` column present
- Long-lived form edit and another update
- Jobs updating the same record
- Stale object kept in memory

## Quick fix

1. `rescue ActiveRecord::StaleObjectError` then reload and retry or notify the user
2. Use atomic updates (`update_all`, `increment!`) for counters
3. Remove `lock_version` if not needed

## Detailed fix by platform

### Rails

1. `begin; order.update!(attrs); rescue ActiveRecord::StaleObjectError; order.reload; retry; end`

## How to diagnose

1. **Concurrency** — Two writers?
2. **Column** — lock_version?
3. **Handling** — Reload/retry?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/ruby/activerecord-staleobjecterror)
