NameError: uninitialized constant (rails)

后端 未结 10 653
一向
一向 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 05:54

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

    0 讨论(0)
  • 2020-12-24 05:55

    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

    0 讨论(0)
  • 2020-12-24 05:57

    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.

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

    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.

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

    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.

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

    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

    0 讨论(0)
提交回复
热议问题