Ruby Error

ActiveRecord::RecordNotFound

ActiveRecord::RecordNotFound is raised when using find(), find_by!(), or first!/last! methods with an ID or conditions that match no records in the database.

Common causes

  • Using find() with non-existent ID
  • Record was deleted but still referenced
  • URL manipulation with invalid IDs
  • Stale data in cache or session
  • Race condition where record is deleted mid-request

How to fix it

  • Use find_by() which returns nil instead of raising
  • Rescue the exception and show 404 page
  • Validate record existence before operations
  • Use where().first for optional records
  • Implement proper 404 handling in controller

Example

ActiveRecord::RecordNotFound example
# Error example
User.find(999)  # => ActiveRecord::RecordNotFound

# Fix with find_by (returns nil)
user = User.find_by(id: 999)
if user
  # process user
else
  # handle missing user
end

# Controller rescue
class UsersController < ApplicationController
  rescue_from ActiveRecord::RecordNotFound, with: :not_found

  private
  def not_found
    render file: 'public/404.html', status: :not_found
  end
end

Track ActiveRecord::RecordNotFound with Checkend

Checkend automatically captures ActiveRecord::RecordNotFound errors in your Ruby application with full context:

  • Complete backtrace with syntax highlighting
  • Request context (URL, params, headers)
  • Automatic grouping of similar errors
  • Instant notifications when errors occur

Stop debugging in production

Get full error context and fix issues faster with self-hosted error tracking.