Rails 发现未返回数据库中存在的记录
这是我遇到的一件奇怪的事情,我想知道人们是否可以给我一些关于在哪里寻找问题的指示。
我正在做的是在模型上运行 id 查找。当我在一组特定记录上运行它时,它们会按预期返回该记录。但是我可以在数据库 (mysql) 中看到另外 100 条左右的记录,但运行 find 命令会返回 ActiveRecord::RecordNotFound 异常。
如果我直接在数据库中创建另一条记录,我也无法提取该记录。但是,如果我在 Rails 控制台中执行新命令,则可以检索该记录。如果我执行 find_by_sql 命令,则可以检索记录。
我尝试过优化表格,但除此之外,我有点困惑是什么导致了这种情况。我在本地计算机上运行 Rails 2.3.12、Ruby 1.8.7 和 mysql gem。任何有关可能原因的想法将不胜感激。谢谢!
编辑感谢您的指点-将记录器附加到我的控制台实例,这暴露了问题-忘记了default_scope,它限制了返回的内容。
This is a weird thing I've been encountering and I was wondering if people could give me a few pointers on where to look for problems.
What I'm doing is running a find for an id on a model. When I run it on a certain set of records, they return that record, as expected. But there's another 100 records or so, that I can see in the database (mysql), but running a find command returns an ActiveRecord::RecordNotFound exception.
If I create another record directly in the database, I cannot pull up that record either. However, if I do a new command in the rails console, that record is retrievable. If I do a find_by_sql command, the records are retrievable.
I've tried optimizing the table, but beyond that, I'm kind of stumped as to what would be causing this. I'm running Rails 2.3.12 with Ruby 1.8.7 and the mysql gem on my local machine. Any thoughts on possible causes would be greatly appreciated. Thanks!
Edit Thanks for the pointers - attached a logger to my console instance, which exposed the problem - a forgotten default_scope which was restricting what was returned.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的模型中一定有某些东西限制了 SQL 查询的范围。
检查 Ruby 代码生成的 SQL 日志,您会发现有关问题所在的有用提示。
There must be something in your model that restraint the scope of your SQL queries.
Check the log for SQL produced by your Ruby code and you will find useful hints on what's going wrong.