Mongoid + YAML + Rails = Conflict

May 14, 2011

The Problem

You get the following when you try to run your Rails app with Mongoid:

db_name must be a string or symbol
C:/Ruby192/lib/ruby/gems/1.9.1/gems/mongo-1.3.1/lib/mongo/util/support.rb:50:in `validate_db_name'

The Cause

You probably have RubyGems v1.5.0, which changes the default yaml parser to psych (as opposed to syck), which doesn’t like :<< in yaml files.

The Solution

Add the following to the top of your config/environment.rb:

require 'yaml' 
YAML::ENGINE.yamler= 'syck'

… or perhaps you could just tweak your YAML to make psych happy. I don’t know what-the-hell I’m doing, so I took the easy way out.

Feel free to share better alternatives.

Credit

Thanks go to the chaps on this Mongoid forum thread.

Discussion, links, and tweets

I'm a developer at LivingSocial. Follow me on Twitter.