NameError: uninitialized constant Ruby / Rails Origin: Application & API Last updated 5 Nov 2026

Ruby: NameError: uninitialized constant UsersController::User (Did you mean? Users)

Ruby (or Rails autoloading) couldn't find a class or module with that name in the current scope.

Exact message you may see NameError: uninitialized constant UsersController::Useruninitialized constant Foo::BarNameError: uninitialized constant Useruninitialized constant ActiveRecord::Base (Rails not loaded)NameError (uninitialized constant)
Seen on: Ruby

Meaning

Causes: a typo, a missing require, a file/class name mismatch (Zeitwerk expects user_profile.rb → UserProfile), a namespace problem (UsersController::User means lookup inside the controller), or a gem not required. In Rails run bin/rails zeitwerk:check.

Common causes

  • Typo or wrong capitalisation
  • File name doesn't match the constant (autoload)
  • Missing require/gem not loaded
  • Constant looked up inside the wrong namespace (use ::User)
  • Class defined in a file not autoloaded (lib/ not in autoload paths)

⚡ Quick fix

  1. Check the constant's file/class name match
  2. bin/rails zeitwerk:check
  3. Use ::User for top-level constants inside namespaces; add require for non-Rails code

Detailed fix by platform

Rails

  1. bin/rails zeitwerk:check && bin/rails runner 'p User'

How to diagnose

  1. Name — Spelling/case?
  2. File — Matches class?
  3. Namespace — Lookup scope?

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

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…LoadError: cannot load such file — Ruby: LoadError: cannot load such file -- nokogiri (or: cannot load such file -- bundler/setup) (Ruby / Rails)LoadError: cannot loa…NameError: undefined local variable or method — Ruby: NameError: undefined local variable or method 'x' for main:Object (Ruby / Rails)NameError: undefined …undefined method for nil — Ruby: NoMethodError: undefined method `x' for nil (nil:NilClass) (Ruby / Rails)undefined method for …Zeitwerk::NameError: expected file to define constant — Rails: Zeitwerk::NameError: expected file app/models/user_profile.rb to define constant UserProfile, but didn't (Ruby / Rails)Zeitwerk::NameError: …NameError…

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.