Error when loading YAML config files in Rails

Posted by ZelluX on Stack Overflow See other posts from Stack Overflow or by ZelluX
Published on 2011-06-30T16:36:58Z Indexed on 2012/06/11 4:40 UTC
Read the original article Hit count: 229

Filed under:
|
|
|

I am configuring Rails with MongoDB, and find a strange problem when paring config/mongo.yml file.

config/mongo.yml is generated by executing script/rails generate mongo_mapper:config, and it looks like following:

defaults: &defaults
  host: 127.0.0.1
  port: 27017

development:
  <<: *defaults
  database: tc_web_development

test:
  <<: *defaults
  database: tc_web_test

From the config file we can see the objects development and test should both have a database field. But when it is parsed and loaded in config/initializers/mongo.db,

config = YAML::load(File.read(Rails.root.join('config/mongo.yml')))
puts config.inspect
MongoMapper.setup(config, Rails.env)

the strange thing comes: the output of puts config.inspect is

{"defaults"=>{"host"=>"127.0.0.1", "port"=>27017}, "development"=>{"host"=>"127.0.0.1", "port"=>27017}, "test"=>{"host"=>"127.0.0.1", "port"=>27017}}

which does not contain database attribute. But when I execute the same statements in a plain ruby console, instead of using rails console, mongo.yml is parsed in a right way.

{"defaults"=>{"host"=>"127.0.0.1", "port"=>27017}, "development"=>{"host"=>"127.0.0.1", "port"=>27017, "database"=>"tc_web_development"}, "test"=>{"host"=>"127.0.0.1", "port"=>27017, "database"=>"tc_web_test"}}

I am wondering what may be the cause of this problem. Any ideas? Thanks.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby