Question

I downloaded the mongo_mapper gem and it installed successfully. Now, I am using it in my app and it always throws exception "No file to load mongo_mapper". What is that supposed to mean?

require 'mongo_mapper'

include mongo

UPDATE: After using require 'rubygems' first. My original problem is gone now there is another weird problem:

I get the following:

**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.

I already installed bson_ext but it keeps throwing this exception!

UPDATE 2: bson warning is now gone but I am not able to list the items in the Customers collection.

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
Was it helpful?

Solution

You need to include rubygems before you include a gem.

require 'rubygems'
require 'mongo_mapper'

I'm also pretty sure your next line include mongo is incorrect, you probably want include Mongo. Actually you probably want nothing at all, since your plan is to use MongoMapper, not the driver directly.

Update:

As for the bson_ext thing, it's not an exception, just a warning. Obviously for production use you would want to sort this out and you can do so by ensuring that you have the latest gems installed: sudo gem install mongo bson_ext mongo_mapper which should tell you (as of October 4th, 2010) that it installed mongo 1.1, bson_ext 1.1 and mongo_mapper 0.8.4.

Update 2:

Need more information. Does the customer you expect to appear show up in the mongo shell? How did you insert it? Are you sure the collection names are right?

So, if you used some .NET thing to make the dataset and can't change it now, you can specify the collection name by hand for your MongoMapper document. Like so:

class Customer
  include MongoMapper::Document
  set_collection_name 'Customers'

  # other stuff
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top