ActiveRecord::RecordNotFound: Couldn't find X with 'id'= Ruby / Rails Origin: Application & API Last updated 5 Nov 2026

Rails: ActiveRecord::RecordNotFound: Couldn't find User with 'id'=42

find/find_by! didn't find a row, so Rails raises (and renders a 404 in production).

Exact message you may see ActiveRecord::RecordNotFound: Couldn't find User with 'id'=42ActiveRecord::RecordNotFound (Couldn't find Post with 'id'=7 [WHERE "posts"."user_id" = $1])Couldn't find User without an IDActiveRecord::RecordNotFound: Couldn't find all Users with 'id': (1, 2, 3) (found 2 results, but was looking for 3)
Seen on: Ruby

Meaning

The record was deleted, belongs to another scope (current_user.posts.find), is soft-deleted (default scope), or the ID is wrong/from another environment. Use find_by (returns nil) if absence is normal, and rescue in controllers (rescue_from ActiveRecord::RecordNotFound).

Common causes

  • Record deleted or never existed
  • Scope/default_scope excluding it
  • Wrong ID or environment/database
  • Authorisation scoping (current_user.records)
  • Stale link/bookmark

⚡ Quick fix

  1. Use find_by(id: params[:id]) and handle nil
  2. Check the record in rails console (User.unscoped.find(42))
  3. rescue_from ActiveRecord::RecordNotFound to render a friendly 404

Detailed fix by platform

Rails

  1. bin/rails runner 'p User.unscoped.where(id: 42).exists?'

How to diagnose

  1. ID — Exists?
  2. Scope — Filters it out?
  3. DB — Right environment?

🔧 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):

ActionController::RoutingError: No route matches — Rails: ActionController::RoutingError (No route matches [GET] "/users/new") (Ruby / Rails)ActionController::Rou…ActiveRecord::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::RecordInvalid: Validation failed — Rails: ActiveRecord::RecordInvalid: Validation failed: Email can't be blank, Name is too short (Ruby / Rails)ActiveRecord::RecordI…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…undefined method for nil — Ruby: NoMethodError: undefined method `x' for nil (nil:NilClass) (Ruby / Rails)undefined method for …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.