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

**Code:** `ActiveRecord::RecordNotFound: Couldn't find X with 'id'=` · **Category:** Ruby / Rails · **Seen on:** Ruby

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

## 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?

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