使用MongoMapper与Ruby的异常:“没有文件加载mongo_mapper”

时间:2010-10-04 19:45:41

标签: ruby mongomapper

我下载了mongo_mapper gem并成功安装。现在,我在我的应用程序中使用它,它总是抛出异常“没有文件加载mongo_mapper”。那是什么意思?

require 'mongo_mapper'

include mongo

更新:首先使用require'rubygems'之后。我原来的问题已经消失,现在又出现了另一个奇怪的问题:

我得到以下内容:

**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance.
  You can install the extension as follows:
  gem install bson_ext

  If you continue to receive this message after installing, make sure that the
  bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.

我已经安装了bson_ext,但它一直在抛出这个异常!

更新2:bson警告现已消失,但我无法列出Customers集合中的项目。

require 'rubygems'
require 'mongo_mapper'

include Mongo

MongoMapper.database = 'Northwind'

class Customer
  include MongoMapper::Document

  key :FirstName, String
  key :LastName, String
  key :UserName, String
end


customers = Customer.all

puts customers.count # this always is 0. It should be 1 since there is one item in the Customers collection

puts customers

1 个答案:

答案 0 :(得分:0)

在包含gem之前,您需要包含rubygems。

require 'rubygems'
require 'mongo_mapper'

我也非常确定您的下一行include mongo不正确,您可能需要include Mongo。实际上你可能根本不需要任何东西,因为你的计划是使用MongoMapper,而不是直接使用驱动程序。

更新

关于bson_ext事情,这不是一个例外,只是一个警告。显然,对于生产用途,您可能希望对此进行排序,并且您可以通过确保安装最新的宝石来实现此目的:sudo gem install mongo bson_ext mongo_mapper它应该告诉您(截至2010年10月4日)它安装了mongo 1.1,bson_ext 1.1和mongo_mapper 0.8.4。

更新2:

需要更多信息。您希望出现的客户是否会出现在mongo shell中?你是怎么插入的?你确定收藏品名称是对的吗?

因此,如果您使用某些.NET事物来制作数据集而现在无法更改它,则可以手动为MongoMapper文档指定集合名称。像这样:

class Customer
  include MongoMapper::Document
  set_collection_name 'Customers'

  # other stuff
end