1

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.

ZelluX
  • 57,940
  • 18
  • 67
  • 104

1 Answers1

1

Depending on your system, Ruby may have been compiled with Psych support, which replaces the older Syck parser. The issue you're seeing (which only involves using a "dry" yaml file with the defaults) has already been fixed in Psych, but is not yet in a released Ruby version.

For now, you can either force the YAML parser to use Syck instead of Psych by putting this at the end of your boot.rb (but beware -- a future version of Ruby will no longer include Syck):

YAML::ENGINE.yamler = 'syck'

Or you could just use a non-DRY YAML file (without the defaults) for the time being.

Dylan Markow
  • 117,383
  • 23
  • 273
  • 197