MongoMapper - manually setting an ObjectId failing with "illegal ObjectID format"
I've got a simple model object:
class UserRating
include MongoMapper::EmbeddedDocument
key :idea_id, ObjectId
key :rating, Integer
end
I'm trying to set an Idea_Id on this object with: user_rating.idea_id = ObjectId.new
This throws: "illegal ObjectID format"
This sure seems like simple code... The only oddity I am noticing is that ObjectID != ObjectId. That could just be a problem with the error message. Not sure. Very simple code. No idea why I can't make it work. If it helps, this is in the context of a Rails 3 Beta 4 project inside of a Cucumber test. I am开发者_运维百科 hitting the mongodb daemon successfully, so there's not a weird connection issue. Would really appreciate any pointers.
MongoMapper has a proxy object called ObjectId - in this case, you want a BSON::ObjectID, which represents an ID as it is stored in mongodb itself.
You probably want:
key :idea_id, BSON::ObjectID, :index => true
No, you want ObjectId. When you assign that you'll want to pass an actual object id which gets generated for each MM model.
user_rating.idea_id = idea.id
精彩评论