views:

154

answers:

2

In order to find a Root Document that contains a embedded document using MongoID/Rails 3 I need to do my query this way:

QuoteRequest.where( "order_request_items._id" => BSON::ObjectID(params[:id]) ).first

Is there a way to query without using the BSON::ObjectID ?

Thanks!

A: 

I'm not a MongoID/Rails user, but my guess is that you can't.

Even in the Mongo shell you have to use ObjectId() if you want to compare ObjectIDs. Something like this won't return any results:

db.foo.find({_id: "4c7ca651db48000000002277"})

You'll have to create an actual ObjectID from the string in order to get results:

db.foo.find({_id: ObjectId("4c7ca651db48000000002277")})

MongoID apparently doesn't automatically convert your input to ObjectIDs. But perhaps there's a way to tell MongoID which fields it should always convert to ObjectIDs? Then you would be able to omit the use of BSON::ObjectID.

Niels van der Rest
I was thinking that MongoID should automatically convert ids into ObjectIDs. It kind of do that in Rails when you do Model.find("4c7ca651db48000000002277") but you need to use the BSON::ObjectID(params[:id]) when doing a conditional search.
Carlos A. Cabrera
A: 

This is a bug, the ids should be automagically converted by Mongoid. You should open a ticket on github: http://github.com/mongoid/mongoid/issues

Nader