简体   繁体   中英

Rails NameError: uninitialized constant on initial load

I have a weird recurring error: every time a worker is booted, it fails on the first attempt and then will work from there on. Each time I get the same error.

NameError: uninitialized constant Models::ScorecardVirtualAttributes::LevelCalculator

So I ran through the usual checks: first I checked to see if it was spelt right. Then I checked to see if it was dependent on a gem that was not included somehow, which it is not.

I think I have replicated the failure in the console. On the initial boot up of the console I run this:

[1] toolkit »  defined? Models::ScorecardVirtualAttributes::LevelCalculator
  => nil
[2] toolkit »  defined? ::Models::ScorecardVirtualAttributes::LevelCalculator
  => nil
[3] toolkit »  Models::ScorecardVirtualAttributes::LevelCalculator
  => Models::ScorecardVirtualAttributes::LevelCalculator < Object
[4] toolkit »  defined? ::Models::ScorecardVirtualAttributes::LevelCalculator
  => "constant"

As you can see, when I check to see if it is defined it fails initially. But then after calling it directly it returns the expected "constant" when I check to see if it is defined.

I then thought that perhaps it's not being loaded correctly. Here's what I have in my config/application.rb :

# Within config/application.rb
...
config.autoload_paths += %W( #{Rails.root}/lib) #/models

I have the lib/models directory added to the autoload_paths , and the model that doesn't seem to be loading initially is in lib/models/scorecards_virtual_attributes/level_calculator.rb .

Is there perhaps something else that could cause this? Or is there something wrong with what I am doing?

Rails 3 doesn't autoload files under the lib directory itself. If you are using Rails 3 or above, you will have to load it in config/application.rb.

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