开发者

Getting Mongoid from params array

In order to find a Root Document that contains a开发者_如何学C 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!


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜