Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what order are the config files loaded for Rails?

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!

like image 850
plainjimbo Avatar asked Dec 29 '11 20:12

plainjimbo


People also ask

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.


1 Answers

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

  1. railties/exe/rails
  2. railties/lib/rails/app_loader.rb
  3. bin/rails
  4. config/boot.rb
  5. rails/commands.rb
  6. rails/command.rb
  7. actionpack/lib/action_dispatch.rb
  8. rails/commands/server/server_command.rb
  9. Rack: lib/rack/server.rb
  10. config/application
  11. Rails::Server#start
  12. config/environment.rb
  13. config/application.rb

Load

  1. railties/lib/rails/all.rb
  2. Back to config/environment.rb
  3. railties/lib/rails/application.rb
  4. 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:

  1. bin/rails
  2. config/boot.rb
  3. rails/commands.rb
  4. actionpack/lib/action_dispatch.rb
  5. rails/commands/server.rb
  6. Rack: lib/rack/server.rb
  7. config/application
  8. Rails::Server#start
  9. config/environment.rb
  10. config/application.rb
  11. railties/lib/rails/all.rb
  12. Back to config/environment.rb
  13. railties/lib/rails/application.rb
  14. 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:

  1. bin/rails railties/lib/rails/cli.rb script/rails config/boot.rb
  2. rails/commands.rb actionpack/lib/action_dispatch.rb
  3. activesupport/lib/active_support.rb
  4. activesupport/lib/active_support/lazy_load_hooks.rb
  5. activesupport/lib/active_support/inflector/methods.rb
  6. actionpack/lib/action_dispatch.rb cont’d. rails/commands/server.rb
  7. Rack: lib/rack/server.rb Rails::Server#start config/environment.rb
  8. config/application.rb Loading Rails railties/lib/rails/all.rb
  9. railties/lib/rails.rb railties/lib/rails/ruby_version_check.rb
  10. active_support/core_ext/kernel/reporting.rb
  11. active_support/core_ext/logger.rb railties/lib/rails/application.rb
  12. active_support/file_update_checker.rb railties/lib/rails/plugin.rb
  13. railties/lib/rails/engine.rb railties/lib/rails/railtie.rb
  14. railties/lib/rails/initializable.rb
  15. railties/lib/rails/configuration.rb
  16. activesupport/lib/active_support/deprecation.rb
  17. activesupport/lib/active_support/deprecation/behaviors.rb
  18. activesupport/lib/active_support/notifications.rb
  19. activesupport/core_ext/array/wrap
  20. activesupport/lib/active_support/deprecation/reporting.rb
  21. activesupport/lib/active_support/deprecation/method_wrappers.rb
  22. activesupport/lib/active_support/deprecation/proxy_wrappers.rb
  23. active_support/ordered_options railties/lib/rails/paths.rb
  24. railties/lib/rails/rack.rb
  25. activesupport/lib/active_support/inflector.rb
  26. active_support/inflections
  27. activesupport/lib/active_support/inflector/transliterate.rb Back to
  28. railties/lib/rails/railtie.rb railties/lib/rails/engine/railties.rb
  29. Back to railties/lib/rails/engine.rb Back to
  30. railties/lib/rails/plugin.rb Back to
  31. railties/lib/rails/application.rb railties/lib/rails/version.rb
  32. activesupport/lib/active_support/railtie.rb
  33. activesupport/lib/active_support/i18n_railtie.rb
  34. railties/lib/rails/railtie/configuration.rb Back to
  35. activesupport/lib/active_support/i18n_railtie.rb Back to
  36. activesupport/lib/active_support/railtie.rb
  37. activesupport/lib/action_dispatch/railtie.rb
  38. activesupport/lib/action_dispatch.rb activemodel/lib/active_model.rb
  39. activesupport/lib/active_support/i18n.rb Back to
  40. activesupport/lib/action_dispatch.rb Back to
  41. activesupport/lib/action_dispatch/railtie.rb Back to
  42. railties/lib/rails.rb Back to railties/lib/rails/all.rb
  43. activerecord/lib/active_record/railtie.rb
  44. activerecord/lib/active_record.rb Back to
  45. activerecord/lib/active_record/railtie.rb
  46. actionpack/lib/action_controller/railtie.rb
  47. 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.

  1. config/preinitializer.rb
  2. config/environment.rb
  3. config/environments/#{RAILS_ENV}.rb
  4. plugin initialization
  5. gem initialization
  6. config/initializer/*.rb
  7. all after_initialize blocks, in the order they were defined in (so same order as above)
  8. 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

like image 165
plainjimbo Avatar answered Nov 13 '22 02:11

plainjimbo