我正在玩rails和mongodb并遇到find_by在找不到结果时返回错误的问题。我已经三次检查以确保它在选项下而不是会话/选项。
有人可以帮忙吗?谢谢 已经检查以确保它在选项下设置,而不是会话选项
错误
Mongoid::Errors::DocumentNotFound:
Problem:
Document not found for class VinDecode with attributes {:numOfDoors=>"3"}.
Summary:
When calling VinDecode.find_by with a hash of attributes, all attributes provided must match a document in the database or this error will be raised.
Resolution:
Search for attributes that are in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error.
下面是我的mongoid.yml
development:
sessions:
default:
database: myapp_development
hosts:
- localhost:27017
options:
options:
raise_not_found_error: false
test:
sessions:
default:
database: myapp_test
hosts:
- localhost:27017
options:
read: primary
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0
答案 0 :(得分:3)
我认为你有两个问题:
1)您需要取消缩进选项
2)您应该在测试声明下面移动您的选项
我遇到了同样的问题,它在我的生产代码中工作,但在我的测试环境中没有工作,解决方案是因为选项是在测试声明之前定义的。
如果您打算为我的强烈推荐的代码编写测试,则应移动以下选项
development:
sessions:
default:
database: myapp_development
hosts:
- localhost:27017
options:
test:
sessions:
default:
database: myapp_test
hosts:
- localhost:27017
options:
read: primary
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0
options:
raise_not_found_error: false