LoginSignup
11
3

More than 5 years have passed since last update.

Railsでlibディレクトリを使うときはconfig.paths.addを使おう

Posted at

TL;DR

http://blog.arkency.com/2014/11/dont-forget-about-eager-load-when-extending-autoload/
これ読んでください。

概要

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

こんな感じのコードありますよね。
これだと、たしかにautoloadはされるんですが、eager_loadはされないのです。なので、eager_loadが有効になっている本番環境などで、状況によってはNameErrorが起きてしまう。
代わりに、

config/application.rb
config.paths.add "#{Rails.root}/lib", eager_load: true

と書くとeager_loadもされるので、本番環境でもエラーが起きず安心、というわけです。

余談

autoloadeager_loadでアンスコあるなしを統一してほしい…

11
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
3