当我按ID搜索时,我似乎只有一个特定的型号有这个问题
>> Cart.where(:_id => '4dae5902e1607c232c000009').first
=> #<Cart _id: 4dae5902e1607c232c000009, _id: BSON::ObjectId('4dae5902e1607c232c000009'), _type: nil>
>> Cart.find('4dae5902e1607c232c000009')
Mongoid::Errors::DocumentNotFound: Document not found for class Cart with id(s) 4dae5902e1607c232c000009.奇怪的是,对于其他模型,我可以使用find很好。有什么想法吗?
栈的其余部分是..。
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:192:in `execute_or_raise'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:190:in `tap'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:190:in `execute_or_raise'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/criterion/inclusion.rb:106:in `find'
from /Library/Ruby/Gems/1.8/gems/mongoid-2.0.1/lib/mongoid/finders.rb:67:in `find'
from (irb):37发布于 2011-04-20 21:15:50
通常情况下,问题是相反的。where失败而find正常工作。
这是因为在查询之前没有将id转换为BSON::ObjectId。
通常你必须这样做
Cart.where(:_id => BSON::ObjectId('4dae5902e1607c232c000009')).first这使我相信您的is存储为字符串,而不是BSON:ObjectId,这可以解释为什么find失败(它搜索的是BSON::ObjectId,而不是字符串)
还可以解释为什么它只是一个模型,因为它完全取决于对象的存储方式。
希望这能有所帮助
https://stackoverflow.com/questions/5725469
复制相似问题