mongoid 的 gem-install 在 Ruby 中抛出一个未初始化的常量,在 irb 中工作 [英] gem-install of mongoid throws an uninitialized constant in Ruby, works in irb
问题描述
我正在用 Ruby/MongoDB 编写一个存储推文的脚本.在我安装了 gem 之后 mongoid,这个第一步代码抛出了一个错误:
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
注意.这不是 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
它在 irb 中工作,我正在运行 ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0].
It works in irb and I'm running ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0].
我做错了什么?
不知何故,我让它工作了.唉,快一年过去了,我真的不记得是怎么回事了.下次我会努力做得更好.
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.
推荐答案
我在尝试让 Bullet
gem 正常工作时遇到了同样的问题.我的解决方案是简单地将 gem 'mongoid', github: 'mongoid/mongoid'
作为 Gemfile 的第一行.我觉得这很奇怪,但这就是我摆脱那个错误的方式.
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.
我使用的是 Ruby 2.1.0 和 Rails 4.0.0
I am using Ruby 2.1.0 and Rails 4.0.0
这篇关于mongoid 的 gem-install 在 Ruby 中抛出一个未初始化的常量,在 irb 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!