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

Peter P. picture Peter P. · Jul 3, 2014 · Viewed 11.6k times · Source

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?

Answer

MIdhun Krishna picture MIdhun Krishna · Jul 3, 2014

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.

Edit: I wrote a blog about Zeitwerk, the new code reloader in Rails. Check it out at -> https://blog.bigbinary.com/2019/10/08/rails-6-introduces-new-code-loader-called-zeitwerk.html