MongoMapper - 手动设置 ObjectId 因“非法 ObjectID 格式”而失败
我有一个简单的模型对象:
class UserRating
include MongoMapper::EmbeddedDocument
key :idea_id, ObjectId
key :rating, Integer
end
我尝试使用以下方法在该对象上设置 Idea_Id: user_ating.idea_id = ObjectId.new
这会抛出:“非法的 ObjectID 格式”
这看起来确实是简单的代码...我注意到的唯一奇怪之处是 ObjectID != ObjectId。这可能只是错误消息的问题。没有把握。非常简单的代码。不知道为什么我不能让它工作。如果有帮助的话,这是在 Cucumber 测试中的 Rails 3 Beta 4 项目的上下文中。我成功访问了 mongodb 守护进程,因此不存在奇怪的连接问题。非常感谢任何指点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MongoMapper 有一个名为 ObjectId 的代理对象 - 在本例中,您需要一个 BSON::ObjectID,它代表存储在 mongodb 本身中的 ID。
您可能想要:
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:
不,您需要 ObjectId。当您指定时,您需要传递为每个 MM 模型生成的实际对象 ID。
用户评级.idea_id = idea.id
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