Last Updated: February 25, 2016
·
1.857K
· sshingler

Don't try to use Mongoid 3.0 on Ruby 1.9.2

If you do, you're likely to see:

NoMethodError: undefined method `[]' for nil:NilClass
from /.rvm/gems/ruby-1.9.2-p290/gems/moped-    1.1.0/lib/moped/node.rb:74:in `block in command'
from /.rvm/gems/ruby-1.9.2-p290/gems/moped-1.1.0/lib/moped/node.rb:522:in `[]'
from /.rvm/gems/ruby-1.9.2-p290/gems/moped-1.1.0/lib/moped/node.rb:522:in `block (3 levels) in flush'
from /.rvm/gems/ruby-1.9.2-p290/gems/moped-1.1.0/lib/moped/node.rb:521:in `map'
from /.rvm/gems/ruby-1.9.2-p290/gems/moped-1.1.0/lib/moped/node.rb:521:in `block (2 levels) in flush'
from /.rvm/gems/ruby-1.9.2-p290/gems/moped-1.1.0/lib/moped/node.rb:113:in `ensure_connected'
from /.rvm/gems/ruby-1.9.2-p290/gems/moped-1.1.0/lib/moped/node.rb:517:in `block in flush'

I was trying a very simple:

config

test:
sessions:
default:
hosts:
- flame.mongohq.com:27049
database: politico
username:<username>
password: <password>

model

class Keyword
include Mongoid::Document
include Mongoid::Timestamps
field :keyword
validatespresenceof :keyword
end

execution

Mongoid.load!('config/mongoid.yml')
keyword = Keyword.create!(:keyword => "test#{Time.now}")

Solution: use Ruby 1.9.3 or roll back to Mongoid 2.4

More details here: http://mongoid.org/en/mongoid/docs/tips.html#ruby