[Ruby on Rails 3.1] How to Enable Autoload for Custom Libraries in the lib Directory

Tadashi Shigeoka ·  Sun, February 26, 2012

I looked up the configuration method to place custom libraries under the lib directory and enable autoload in Ruby on Rails 3.1, so taking notes.

In Rails 3, autoload is disabled by default, so you need to enable autoload in config/application.rb.

■ config/application.rb:

# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths += %W(#{config.root}/lib)  # 追加
config.autoload_paths += Dir["#{config.root}/lib/**/"] # 追加

This configuration enabled autoload for me, but if this doesn’t work, you might want to reference this:

自作ライブラリをrequireするためにやったこと - 喉鳴らす未来に達し

That’s all from the Gemba regarding Rails autoload configuration.

【References】

Rails3でautoloadはデフォルトで無効になっている - INOHILOG

Rails 3 Quicktip: Autoload lib directory including all subdirectories, avoid lazy loading

Rails 3 autoload - Stack Overflow

Ruby/Ruby on Rails/libディレクトリとオートロードと自作ライブラリ - TOBY SOFT wiki