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

你说的曾经没有我的故事 提交于 2019-12-13 08:37:21

问题


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:


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



来源:https://stackoverflow.com/questions/7371003/gem-install-of-mongoid-throws-an-uninitialized-constant-in-ruby-works-in-irb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!