← back to the blog
fousa blog
17
FEB
Nasty load problem with Rails 3
The following line of code loads the data from config/config.yml file into a global CONFIG variable. The piece of code below is executed by the initializer initializers/load_config.rb.
CONFIG = File.open(RAILS_ROOT + "/config/config.yml") { |file| YAML::load(file) }This works great for Rails 2.x.x. But in Rails3 it fails...
SOLUTION
This is what I did:
- Changed RAILS_ROOT to Rails.root (the new way in Rails3)
- Change the string concatenation
And this is the result code:
CONFIG = File.open("#{Rails.root}/config/config.yml") { |file| YAML::load(file) }When using "+", the path was /config/config.yml. But then I placed the Rails.root inside the string with "#{}" and the path became /Users/jelle/Sites/fousa/config.config.yml.
Some things I just don't understand!
UPDATE
This is probably the best way to do this. It has been suggested to me by Jacques Crocker.
CONFIG = Rails.root.join("config", "config.yml").open{ |file| YAML::load(file) }Send me some feedback!