开发者

gem-install of mongoid throws an uninitialized constant in Ruby, works in irb

开发者 https://www.devze.com 2023-04-04 10:53 出处:网络
I am writing a script with Ruby/MongoDB that stores Tweets. After I gem-installed mongoid, this first-steps code throws an error:

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号