ActiveRecord::StaleObjectError (optimistic locking) Ruby / Rails Origin: Application & API Last updated 5 Nov 2026

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

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

Exact message you may see ActiveRecord::StaleObjectError: Attempted to update a stale object: OrderAttempted to update a stale object: UserActiveRecord::StaleObjectError (Attempted to update a stale object)optimistic locking stale object Rails
Seen on: Ruby

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?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

1213 — MySQL ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction (MySQL)1213ActiveRecord::AssociationTypeMismatch — Rails: ActiveRecord::AssociationTypeMismatch: User(#1234) expected, got "42" which is an instance of String(#5678) (Ruby / Rails)ActiveRecord::Associa…ActiveRecord::ConnectionNotEstablished — Rails: ActiveRecord::ConnectionNotEstablished: No connection pool for 'ActiveRecord::Base' found (Ruby / Rails)ActiveRecord::Connect…ActiveRecord::Deadlocked: PG::TRDeadlockDetected — Rails: ActiveRecord::Deadlocked: PG::TRDeadlockDetected: ERROR: deadlock detected (Ruby / Rails)ActiveRecord::Deadloc…ActiveRecord::ReadOnlyRecord — Rails: ActiveRecord::ReadOnlyRecord: Order is marked as readonly (Ruby / Rails)ActiveRecord::ReadOnl…ActiveRecord::RecordNotUnique: duplicate key value — Rails: ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_email" (Ruby / Rails)ActiveRecord::RecordN…PG::ConnectionBad: could not connect to server — Rails: PG::ConnectionBad: could not connect to server: Connection refused. Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? (Ruby / Rails)PG::ConnectionBad: co…VersionError: No matching document found for id — Mongoose: VersionError: No matching document found for id "5f..." version 3 modifiedPaths "items" (MongoDB)VersionError: No matc…ActiveRec…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.