gem-install of mongoid throws an uninitialized con

2019-08-27 03:26发布

I am writing a script with Ruby/MongoDB that stores Tweets. After I gem-installed mongoid, this first-steps code throws an error:

require 'rubygems'
require 'mongo'
require 'mongoid'

Mongoid.database = Mongo::Connection.new('localhost').db('db')
# snippet from http://rujmah.posterous.com/using-mongoid-without-rails

NB. This is no Rails app, but a Terminal script. The error I get is:

./mongoid.rb:10: uninitialized constant Mongoid (NameError)
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
    from mongoid.rb:3

It works in irb and I'm running ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0].

What am I doing wrong?

Edit August 2012

Somehow I got it to work. Alas, nearly a year on, I really can’t remember how. I will try to do better next time.

1条回答
成全新的幸福
2楼-- · 2019-08-27 04:06

I hit the same issue while trying to get Bullet gem to work. The solution for me was to simply move gem 'mongoid', github: 'mongoid/mongoid' as the first line of the Gemfile. I find it really odd but that was how I got rid of that error.

I am using Ruby 2.1.0 and Rails 4.0.0

查看更多
登录 后发表回答