I'm in the process of trying to initialize different gems for different environments. I'm using initializer config files (for things like Paperclip) and environment config files (for my dev, test, qa, prod environments).
For some context, I'm trying to get my prod and qa servers to use S3 storage for Paperclip, but use local storage with different directories for dev and test. I have no idea in what order these config files are loaded.
I was wondering if someone could shed some light on the load order so that I can make sure I've got any dependencies or overrides correct. Also, I just like to know how these things work.
I'm particularly interested in the directories/files listed below
config/ environments/ develop.rb test.rb ...env-specific config files initializers/ paperclip.rb ...gem-specific config files application.rb boot.rb deploy.rb environment.rb routes.rb
Thanks!
Where is Rails config?
You probably know that you can configure Rails in config/application. rb and config/environments/development. rb etc. But you can also leverage that for configuring your own custom settings for your application.
What is Rails application config?
In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The configuration file config/application. rb and environment-specific configuration files (such as config/environments/production.
What are Initializers in Rails?
An initializer is any file of ruby code stored under /config/initializers in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.
What is the purpose of environment RB and application RB file?
In the environment. rb file you configure these run-levels. For example, you could use it to have some special settings for your development stage, which are usefull for debugging. The purpose of this file is to configure things for the whole application like encoding.
Rails 6.0
Updated Nov 2019: Initialization Process and Configuration
Same as Rails 5.2
Rails 5.2
Updated Nov 2019:Initialization Process Configuration
Launch
- railties/exe/rails
- railties/lib/rails/app_loader.rb
- bin/rails
- config/boot.rb
- rails/commands.rb
- rails/command.rb
- actionpack/lib/action_dispatch.rb
- rails/commands/server/server_command.rb
- Rack: lib/rack/server.rb
- config/application
- Rails::Server#start
- config/environment.rb
- config/application.rb
Load
- railties/lib/rails/all.rb
- Back to config/environment.rb
- railties/lib/rails/application.rb
- Rack: lib/rack/server.rb
Rails 4.2
Updated Sep 2013: For Rails 4 it appears to have changed again. There is now a Rails-4 Guide on The Rails Initialization Process. You'll notice that this list is much shorter than the one for Rails 3. I'm not sure if they removed some of the depth or what... Haven't had the time to go over it all:
- bin/rails
- config/boot.rb
- rails/commands.rb
- actionpack/lib/action_dispatch.rb
- rails/commands/server.rb
- Rack: lib/rack/server.rb
- config/application
- Rails::Server#start
- config/environment.rb
- config/application.rb
- railties/lib/rails/all.rb
- Back to config/environment.rb
- railties/lib/rails/application.rb
- Rack: lib/rack/server.rb
For more detailed information on how to configure some of these files see the Rails-4 Guide on Configuring Rails Applications
Rails 3.2
Updated Sep 2013: For Rails 3 it appears to have changed a lot. There is now a Rails-3 Guide on The Rails Initialization Process:
- bin/rails railties/lib/rails/cli.rb script/rails config/boot.rb
- rails/commands.rb actionpack/lib/action_dispatch.rb
- activesupport/lib/active_support.rb
- activesupport/lib/active_support/lazy_load_hooks.rb
- activesupport/lib/active_support/inflector/methods.rb
- actionpack/lib/action_dispatch.rb cont’d. rails/commands/server.rb
- Rack: lib/rack/server.rb Rails::Server#start config/environment.rb
- config/application.rb Loading Rails railties/lib/rails/all.rb
- railties/lib/rails.rb railties/lib/rails/ruby_version_check.rb
- active_support/core_ext/kernel/reporting.rb
- active_support/core_ext/logger.rb railties/lib/rails/application.rb
- active_support/file_update_checker.rb railties/lib/rails/plugin.rb
- railties/lib/rails/engine.rb railties/lib/rails/railtie.rb
- railties/lib/rails/initializable.rb
- railties/lib/rails/configuration.rb
- activesupport/lib/active_support/deprecation.rb
- activesupport/lib/active_support/deprecation/behaviors.rb
- activesupport/lib/active_support/notifications.rb
- activesupport/core_ext/array/wrap
- activesupport/lib/active_support/deprecation/reporting.rb
- activesupport/lib/active_support/deprecation/method_wrappers.rb
- activesupport/lib/active_support/deprecation/proxy_wrappers.rb
- active_support/ordered_options railties/lib/rails/paths.rb
- railties/lib/rails/rack.rb
- activesupport/lib/active_support/inflector.rb
- active_support/inflections
- activesupport/lib/active_support/inflector/transliterate.rb Back to
- railties/lib/rails/railtie.rb railties/lib/rails/engine/railties.rb
- Back to railties/lib/rails/engine.rb Back to
- railties/lib/rails/plugin.rb Back to
- railties/lib/rails/application.rb railties/lib/rails/version.rb
- activesupport/lib/active_support/railtie.rb
- activesupport/lib/active_support/i18n_railtie.rb
- railties/lib/rails/railtie/configuration.rb Back to
- activesupport/lib/active_support/i18n_railtie.rb Back to
- activesupport/lib/active_support/railtie.rb
- activesupport/lib/action_dispatch/railtie.rb
- activesupport/lib/action_dispatch.rb activemodel/lib/active_model.rb
- activesupport/lib/active_support/i18n.rb Back to
- activesupport/lib/action_dispatch.rb Back to
- activesupport/lib/action_dispatch/railtie.rb Back to
- railties/lib/rails.rb Back to railties/lib/rails/all.rb
- activerecord/lib/active_record/railtie.rb
- activerecord/lib/active_record.rb Back to
- activerecord/lib/active_record/railtie.rb
- actionpack/lib/action_controller/railtie.rb
- actionpack/lib/action_view.rb
For more detailed information on how to configure some of these files see the Rails-3 Guide on Configuring Rails Applications
Rails 2.3
Originally (Dec 2011), I stumbled across a blog post that had an awesome explanation of How the Initialization Process Worked for Rails 2.
- 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
For more detailed information on how to configure some of these files see the Rails-2 Guide on Configuring Rails Applications