NameError: uninitialized constant (rails)

后端 未结 10 654
一向
一向 2020-12-24 05:52

I have a simple model called PhoneNumber:

class PhoneNumber < ActiveRecord::Base
  validates :pnumber, presence: true, on: :create #=> { :message =>         


        
相关标签:
10条回答
  • 2020-12-24 06:11

    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

    0 讨论(0)
  • 2020-12-24 06:18

    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.

    0 讨论(0)
  • 2020-12-24 06:19

    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

    0 讨论(0)
  • 2020-12-24 06:20

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

    spring stop
    spring binstub --all
    spring start
    rails s
    
    0 讨论(0)
提交回复
热议问题