Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autoload files in folder under rails app's root

I am trying to have files under myapplication/somefolder. Google and Stackoverflow say I should add this:

config.autoload_paths += %W(#{config.root}/somefolder)

in my config/application.rb, so I did.

But the files don't get loaded.

I tried namig somefolder/myclass.rb both class Myclass and class Somefolder::Myclass but still no luck.

I can see that the dir was found in Rails.application.config.autoload_paths in console does indeed include my /path/to/myapplication/somefolder directory, so that should be okay.

All the other questions around this topic use theapp/app/somefolder or theapp/lib/somefolder but not theapp/somefolder so maybe thats where it gets rotten.

So I tried referencing the class with ::Somefolder::MyClass but not even that helped.

I am using Rails 3.2.1

like image 924
hakunin Avatar asked Jan 29 '26 14:01

hakunin


2 Answers

Just ran into this myself today and decided to dive deep.

The reason you don't see in ActiveSupport::Dependencies.autoload_paths the paths you're adding to config.autoload_paths in config/application.rb is that they aren't copied over until the application is initialized. See rails/engine.rb in the railties gem:

module Rails
  class Engine < Railtie
    …

    # Set the paths from which Rails will automatically load source files,
    # and the load_once paths.
    #
    # This needs to be an initializer, since it needs to run once
    # per engine and get the engine as a block parameter
    initializer :set_autoload_paths, :before => :bootstrap_hook do |app|
      ActiveSupport::Dependencies.autoload_paths.unshift(*_all_autoload_paths)
      ActiveSupport::Dependencies.autoload_once_paths.unshift(*_all_autoload_once_paths)

      # Freeze so future modifications will fail rather than do nothing mysteriously
      config.autoload_paths.freeze
      config.eager_load_paths.freeze
      config.autoload_once_paths.freeze
    end

    …

    def _all_autoload_paths
      @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
    end

    …
  end
end

Were you by any chance trying to invoke MyClass from within config/application.rb or, even earlier, from a script or module that requires config/application.rb? If so, you'll have to explicitly require the file defining MyClass, e.g.:

require File.expand_path('../../somefolder/my_class',  __FILE__)
# now use MyClass
like image 65
Julian Mehnle Avatar answered Feb 01 '26 08:02

Julian Mehnle


The workaround is to go to ActiveSupport::Dependencies.autoload_paths directly.

ActiveSupport::Dependencies.autoload_paths << "#{config.root}/somefolder"

But I am still looking for the reason why config.autoload_paths didn't work so if you post an aswer to that, I'll accept it!

like image 23
hakunin Avatar answered Feb 01 '26 08:02

hakunin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!