devroom.io/content/posts/2010-05-30-upgrading-to-mongoid-beta-6.md

40 lines
1.5 KiB
Markdown
Raw Normal View History

2015-03-26 11:28:08 +00:00
+++
date = "2010-05-30"
title = "Upgrading to Mongoid Beta 6"
tags = ["Ruby", "Rails", "mongoid", "monogdb"]
slug = "upgrading-to-mongoid-beta-6"
+++
If you are working with Rails 3 and Mongoid, you're likely to upgrade to [`mongoid-2.0.0.beta6`][1]. That's okay, but you will run into a few problems. Among others, one will be:
2017-03-20 15:35:19 +00:00
``` text
Database should be a Mongo::DB, not NilClass
```
2015-03-26 11:28:08 +00:00
or
2017-03-20 15:35:19 +00:00
``` text
Mongoid::Errors::InvalidDatabase: Mongoid::Errors::InvalidDatabase
```
2015-03-26 11:28:08 +00:00
Another, Mongoid-related problem is the error `uninitialized constant OrderedHash`.
Luckily, these problems can be solved quite easily.
[1]: http://rubygems.org/gems/mongoid
2017-03-20 15:35:19 +00:00
2015-03-26 11:28:08 +00:00
The first thing you need to do is make sure you use the right version of `bson_ext`. Beta 6 requires you to run `bson_ext-1.0.1` or you'll get the `OrderedHash` error. Okay, with that out of the way, let's focus on the MongoDB errors.
The problem is that Mongoid is accessed/used before it is properly initialized. To resolve this issue, add the following line to your other requires at the top of `config/application.rb`.
2017-03-20 15:35:19 +00:00
``` ruby
require 'mongoid/railtie'
```
2015-03-26 11:28:08 +00:00
With that, you initialize Mongoid correctly. Hope it helps.
Update: I also ran into `keys must be strings or symbols` errors. This is now a _known issue_ with `mongoid-2.0.0.beta6` and has been fixed in `master`. If you are using Bundler (you are, aren't you?) then you can use the master branch instead of the gem:
2017-03-20 15:35:19 +00:00
``` ruby
# gem 'mongoid', '2.0.0.beta6'
gem 'mongoid', :git => 'http://github.com/durran/mongoid.git'
```