Rails2.3的时候, 启动应用后的加载顺序:
- config/preinitializer.rb
- config/environment.rb
- config/environments/#{RAILS_ENV}.rb
- plugin initialization
- gem initialization
- config/initializer/*.rb
- all after_initialize blocks, in the order they were defined in (so same order as above)
- any junk left below the Rails::Initializer.run call/block in environment.rb
在 Rails3 之后, 引入了Bundler,并且移除了preinitializer,加载顺序变成了
- config/application.rb
- gems initialization
- config/environment.rb
- config/environments/#{RAILS_ENV}.rb
- plugin initialization
- your rails app initialization
- config/initializer/*.rb
如果想要在整个Rails项目之前加载代码需要写在config/application.rb中的require ‘rails/all’之前。
参考: