简体   繁体   中英

Rails: NameError: uninitialized constant

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?

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.

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.

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

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

Similar with @Michael-Neal.

I had named the controller as singular. app/controllers/ product_controller.rb

When I renamed it as plural, error solved. app/controllers/ products_controller.rb

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.

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

通过确保文件名与具有正确约定的类名相同,我的问题也得到了解决,例如class AdLikes具有文件名ad_likes.rb

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

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.

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

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

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM