Rails unable to autoload constant from file despite being defined in that file

我怕爱的太早我们不能终老 提交于 2019-12-05 10:25:26

问题


This is a tricky one to explain. I have a module in another module namespace like so:

# app/models/points/calculator.rb
module Points
  module Calculator
    def self.included(base)
      base.send(:include, CommonMethods)
      base.send(:include, "Points::Calculator::#{base}Methods".constantize)
    end
  end
end

So then in other classes all I need to do is:

class User
  include Points::Calculator
end

I've specified this directory in application.rb to be autoloadable...(even though i think rails recurses through models...)

config.autoload_paths += Dir[ Rails.root.join('app', 'models', "points") ]

In development env, everything works fine. When running tests(and production env), I get the following error:

Unable to autoload constant Points::Calculator, expected /Users/pete/work/recognize/app/models/points/calculator.rb to define it (LoadError)

I actually followed the advice here to fix the problem: Stop Rails from unloading a module in development mode by explicitly requiring calculator.rb in application.rb.

However, why is this happening??

I stuck some debug output in ActiveSupport's dependencies.rb file and noticed that this file is being required twice. The first time its required I can see that the constant is indeed loaded.

But the 2nd time its required the constant has been unloaded as far as Rails can tell, but when the actual require is called, ruby returns false because ruby knows its already required it. Then Rails throws the "unable to autoload constant" error because the constant still isn't present and ruby didn't "re-require" the file.

Can anyone shed light on why this might be happening?


回答1:


Rails augments the constant lookup mechanism of ruby.

Constant lookup in Ruby:

Similar to method missing, a Module#constant-missing is invoked when a reference to a constant fails to be resolved. When we refer to a constant in a given lexical scope, that constant is searched for in:

Each entry in Module.nesting 
Each entry in Module.nesting.first.ancestors
Each entry in Object.ancestors if Module.nesting.first is nil or a module.

When we refer to a constant, Ruby first attempts to find it according to this built-in lookup rules.

When ruby fails to find... rails kicks in, and using its own lookup convention and its knowledge about which constants have already been loaded (by ruby), Rails overrides Module#const_missing to load missing constants without the need for explicit require calls by the programmer.

Its own lookup convention?

Contrasting Ruby’s autoload (which requires the location of each autoloaded constant to be specified in advance) rails following a convention that maps constants to file names.

Points::Calculator # =>points/calculator.rb

Now for the constant Points::Calculator, rails searches this file path (ie 'points/calculator.rb') within the autoload paths, defined by the autoload_paths configuration.

In this case, rails searched for file path points/calculator in its autoloaded paths, but fails to find file and hence this error/warning is shown.

This answer is an abstract from this Urbanautomation blog.




回答2:


Calculator should be a class to be autoloaded correctly

module Points
  class Calculator
  ...
  end
end


来源:https://stackoverflow.com/questions/24545602/rails-unable-to-autoload-constant-from-file-despite-being-defined-in-that-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!