views:

238

answers:

2

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.

+2  A: 

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
Chris Heald
A: 

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

John Nunemaker
Wow, the man himself! Thanks for the input John. In my case, I'm receiving the idea's id from a form post. I didn't really want to hit mongo just to get an id I already have. Would it be ok to do something like: user_rating.idea_id = Idea.new(:id => params[:idea_id]).id
Jeff D