Reputation: 7100
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!
Upvotes: 29
Views: 11581
Reputation: 7100
Updated Nov 2019: Initialization Process and Configuration
Same as Rails 5.2
Updated Nov 2019:Initialization Process Configuration
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:
For more detailed information on how to configure some of these files see the Rails-4 Guide on Configuring Rails Applications
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:
For more detailed information on how to configure some of these files see the Rails-3 Guide on Configuring Rails Applications
Originally (Dec 2011), I stumbled across a blog post that had an awesome explanation of How the Initialization Process Worked for Rails 2.
For more detailed information on how to configure some of these files see the Rails-2 Guide on Configuring Rails Applications
Upvotes: 50
Reputation: 1757
This answer is used to supplement the accepted answer, the accepted answer didnt mention when development.rb and other init files load.
In ruby 2.0, I insert following code in bin/rails, then run 'bin/rails s' to view the loading order of local application files. In this example, my project name is bole_api.
files = []
tp = TracePoint.new(:line) do |tp|
if tp.path =~ /bole_api/
unless files.include? tp.path
puts "#{tp.path}".inspect
files.push(tp.path)
end
end
end
tp.enable
and my local result is
"/home/leijing/studio/bole_api/bin/rails"
"/home/leijing/studio/bole_api/config/boot.rb"
"/home/leijing/studio/bole_api/Gemfile"
"/home/leijing/studio/bole_api/config/application.rb"
=> Booting WEBrick
=> Rails 4.1.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
"/home/leijing/studio/bole_api/config.ru"
"/home/leijing/studio/bole_api/config/environment.rb"
"/home/leijing/studio/bole_api/config/environments/development.rb"
"/home/leijing/studio/bole_api/config/initializers/carrierwave.rb"
"/home/leijing/studio/bole_api/config/initializers/cookies_serializer.rb"
"/home/leijing/studio/bole_api/config/initializers/filter_parameter_logging.rb"
"/home/leijing/studio/bole_api/config/initializers/rabl_init.rb"
"/home/leijing/studio/bole_api/config/initializers/session_store.rb"
"/home/leijing/studio/bole_api/config/initializers/wrap_parameters.rb"
"/home/leijing/studio/bole_api/config/routes.rb"
"/home/leijing/studio/bole_api/app/api/bole_app_api.rb"
"/home/leijing/studio/bole_api/app/api/home_api.rb"
"/home/leijing/studio/bole_api/app/api/video_api.rb"
[2014-06-27 11:06:57] INFO WEBrick 1.3.1
[2014-06-27 11:06:57] INFO ruby 2.1.0 (2013-12-25) [x86_64-linux]
[2014-06-27 11:06:57] INFO WEBrick::HTTPServer#start: pid=30157 port=3000
From the above output, you can get a view of the order of how local files loaded. And combine the conclusion of http://edgeguides.rubyonrails.org/initialization.html, you can get a view of whole initialize process.
Upvotes: 17