Why can't I fetch Mongoid documents using my old _id format?
I have a model in a Rails/Mongoid application that I had initially set a key using key: uid
(where :uid is an attribute)
I am removing this so that the model reverts to using the default _id fo开发者_StackOverflowrmat. However the existing objects are not reflecting this change.
for example for an object with uid: 507, the _id is still 507. thus I am able to retrieve it using Testobjects.where(_id: "507")
however, Testobjects.find("507")
returns BSON::InvalidObjectId: illegal ObjectId format
(using BSON::ObjectId('507')
gives the same error)
so now I am unable to retrieve old objects using find (because it's not recognizing their _id, and I can't retrieve a new _id)
any ideas on how to fix this?
Your old keys were probably strings or integers, not BSON::ObjectId instances. You might need to write a migration that makes them consistent, because earlier versions of Mongoid didn't typecast the id.
精彩评论