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).
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) 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
- Use
find_by(id: params[:id])and handle nil - Check the record in
rails console(User.unscoped.find(42)) rescue_from ActiveRecord::RecordNotFoundto render a friendly 404
Detailed fix by platform
Rails
bin/rails runner 'p User.unscoped.where(id: 42).exists?'
How to diagnose
- ID — Exists?
- Scope — Filters it out?
- 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:
Similar errors
- ActionController::RoutingError: No route matches Rails: ActionController::RoutingError (No route matches [GET] "/users/new")
- ActiveRecord::AssociationTypeMismatch Rails: ActiveRecord::AssociationTypeMismatch: User(#1234) expected, got "42" which is an instance of String(#5678)
- ActiveRecord::ConnectionNotEstablished Rails: ActiveRecord::ConnectionNotEstablished: No connection pool for 'ActiveRecord::Base' found
Most viewed in Ruby / Rails
- ActiveRecord::PendingMigrationError Rails: ActiveRecord::PendingMigrationError — Migrations are pending. To resolve this issue, run: bin/rails db:migrate
- ActionController::InvalidAuthenticityToken Rails: ActionController::InvalidAuthenticityToken (Can't verify CSRF token authenticity)
- Bundler could not find compatible versions for gem Bundler: Bundler could not find compatible versions for gem "activesupport": In Gemfile: rails (= 7.1.0) depends on activesupport (= 7.1.0)
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 5 Nov 2026