59

I have a simple model called PhoneNumber:

class PhoneNumber < ActiveRecord::Base
  validates :pnumber, presence: true, on: :create #=> { :message => " cannot be blank" }
  validates :pnumber, numericality: true, on: :create
end

I go to the root folder of the application (the one containing the app sub-folder) and start the console:

rails console --sandbox

When I try to create an empty PhoneNumber (I want to get an error message as the validation shall fail) I am getting the following error message:

2.0.0-p451 :001 > PhoneNumber.new
NameError: uninitialized constant PhoneNumber
from (irb):1
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/app_rails_loader.rb:43:in `require'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/app_rails_loader.rb:43:in `block in exec_app_rails'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/app_rails_loader.rb:32:in `loop'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/app_rails_loader.rb:32:in `exec_app_rails'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/lib/rails/cli.rb:5:in `<top (required)>'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/bin/rails:9:in `require'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/railties-4.1.5/bin/rails:9:in `<top (required)>'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/rails:23:in `load'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/rails:23:in `<main>'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
from /Users/nnikolo/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/2.2.2.02.02.02.0.2.2.02.222222.2.02.02.0.2.2.022222222222222

It seems the console is not aware of the model. In plain ruby you need to 'require' the file containing the class but I thought that the rails console shall automatically load all models. What is going on here?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Nick
  • 2,924
  • 4
  • 36
  • 43
  • 1
    To fix for me, I just had to pluralize the model name in the migration file's name for e.g. `20180612create_users.rb` – fungusanthrax Jul 18 '18 at 20:39

16 Answers16

77

Some things to try:

  1. Restart the rails console; changes to your models will only get picked up by a rails console that is already open if you do > reload! (although I have found this to be unpredictable), or by restarting the console.

  2. Is your model file called "phone_number.rb" and is it in "/app/models"?

  3. You should double-check the "--sandbox" option on your rails console command. AFAIK, this prevents changes. Try it without the switch.

Dan Laffan
  • 1,544
  • 12
  • 12
  • 9
    It seems it was the name of the file - it was app/models/PhoneNumber.rb. When I changed it to app/models/phone_number.rb the error message disappeared. – Nick Oct 16 '14 at 01:06
  • 1
    `reload!` worked for me! Seems like some parts of the model initialization get cached, so if there is an error in that section it can become an "invisibile" error when you run the console a second time. – Meekohi May 16 '17 at 18:34
  • In my case the model was named in plural I just had to change that. Thank you – Olivier JM Mar 13 '20 at 12:20
  • My problem was solved by restarting the rails console. – Indika K Mar 08 '22 at 03:48
  • Just to add on to this; I had a module called `PRH` (an acronym), hence I needed to call the file `p_r_h.rb` for it to work. – Kent Robin Dec 14 '22 at 12:50
31

I was getting the error:

NameError: uninitialized constant

Then I noticed that I had accidentally created a plural model so I went back and renamed the model file to singular and also changed the class name in the model file to singular and that solved it.

Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
bananaforscale
  • 820
  • 10
  • 13
29

I started having this issue after upgrading Rails 5.1 to 5.2
I got it solved with:

spring stop
spring binstub --all
spring start
rails s
user9869932
  • 6,571
  • 3
  • 55
  • 49
8

I ran into this also with a file directly in the models directory, and it turns out that I wasn't properly loading up the code on startup. I was able to fix the issue by setting config.eager_load = true in my development.rb file. This made the class available to me in the console

bradley2w1dl
  • 81
  • 1
  • 2
  • 2
    Here's more information on this solution: http://collectiveidea.com/blog/archives/2016/07/22/solutions-to-potential-upgrade-problems-in-rails-5/ tldr; "With Rails 5, autoloading is now completely disabled if config.eager_load = true." – lostphilosopher Jan 11 '17 at 19:39
  • Currently upgrading a legacy project from 4.2 to 5.2 (as a first step) and this solved a problem we were having with RAILS_EAGER_LOAD_CLASSES: true being set in CI, leading to NameError: Uninitialized Constant. Thank you all! – wbt11a Oct 12 '22 at 00:07
5

If none of the above work, I also have a different approach, as it happened to me in a real scenario.

More specifically using auto-generated Ruby files from Thrift.


In my situation, I had a Module with several classes, so the order is important in this case:

Class A makes use of Class B in the same module. However, Class B was declared after Class A.

Simply making Class B to be declared before Class A solved the issue to me.

Matheus Felipe
  • 2,482
  • 25
  • 24
5

My problem was also solved by making sure that the filename was the same as the class name with the right conventions, that being for example class AdLikes has the filename ad_likes.rb

Jenny
  • 71
  • 1
  • 6
4

Similar to @Michael-Neal.

I had named the controller singular.

app/controllers/product_controller.rb

When I renamed it as plural, the error was solved.

app/controllers/products_controller.rb

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
Uğur Aldanmaz
  • 1,018
  • 1
  • 11
  • 16
2

I had this same issue when working on a Rails 6 application in Ubuntu 20.04.

When I start the rails console using the command

rails console

and then run the command below to list all the available roles

Role.all

I get the error below:

Traceback (most recent call last):
        1: from (irb):2
NameError (uninitialized constant Role)

Here's how I fixed it:

The issue was that my Role model was namespaced with a module called Userbase. So instead of this:

Role.all

It should be this:

Userbase::Role.all

That's all

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
1

I had this problem because I changed the name of the class in a model, and it did not match the name of the file.

"Model class names use CamelCase. These are singular, and will map automatically to the plural database table name.

Model files go in app/models/#{singular_model_name}.rb."

https://gist.github.com/iangreenleaf/b206d09c587e8fc6399e#model

MrMojoRisin
  • 1,349
  • 1
  • 12
  • 9
0

I had the same error. Turns out in my hasty scaffolding I left out the model.rb file.

jared
  • 231
  • 3
  • 13
0

I had a similar error, but it was because I had created a has_one relationship and subsequently deleted the model that it had_one of. I just forgot to delete the has_one relationship from the remaining model.

therealrodk
  • 386
  • 2
  • 10
0

In my case, I named a column name type and tried to set its value as UNPREPARED. And I got an error message like this:

Caused by: api_1 | NameError: uninitialized constant UNPREPARED

In rails, column type is reserved:

ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'UNPREPARED'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Pl ease rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Food.inheritance_column to use another column for that information

glinda93
  • 7,659
  • 5
  • 40
  • 78
0

Hi I got the same problem, I solved it just by specifying the the controller on your /config/routes.rb find the route the put the controller name.

Example: I have the controller product_controller.rb

routes.rb

resource :product, controller: 'product', only: [:edit, :update]
0

I got the same error. There was a naming convention issue with the classes. I had the file name checkitemgroup_query.rb but I was initializing the class as CheckitemGroupQuery. By replacing the class name with CheckitemgroupQuery my issue was resolved.

Archmede
  • 1,592
  • 2
  • 20
  • 37
0

Let me throw out my experience in case it is useful to somebody.

tl;dr; app database was not created in the RDBMS.

All of a sudden I started to get this when I tried to run any test for one class under app/models. It didn't change since ages but I double checked it matched proper file/class name conventions to no avail.

Since backtrace pointed at eager_loading I looked the excellent link provided by @lostphilosopher in a comment above. So I tried disabling eager_loading as a first step.

Run failed with an error that database was not found. After creating the DB both eager and non-eager loading started to work. I have no idea why error was so misleading in the eager mode. But no time now to dig into this.

Just thought somebody may find this useful.

akostadinov
  • 17,364
  • 6
  • 77
  • 85
0

For me it started when upgrading to Rails 7.

It turns out that this error may be thrown if app has autoloaded reloadables during its initialization process.

I found this StackOverflow response which refers to this guide - according to that guide:

Applications that autoloaded reloadable constants during initialization outside of to_prepare blocks got those constants unloaded and had this warning issued since Rails 6.0:
/ deprecation warning message /
If you still get this warning in the logs, please check the section about autoloading when the application boots in the autoloading guide. You'd get a NameError in Rails 7 otherwise.

I had autoloaded reloadables during initialization phase of the app. After wrapping it with

Rails.application.config.to_prepare do and end as suggested in the autoloading guide the issue got resolved.

Ula
  • 2,628
  • 2
  • 24
  • 33