Rails发现没有返回数据库中存在的记录(Rails find not returning a record that exists in the database)

编程入门 行业动态 更新时间:2024-10-25 03:32:59
Rails发现没有返回数据库中存在的记录(Rails find not returning a record that exists in the database)

这是我遇到的一个奇怪的事情,我想知道人们是否可以给我一些关于在哪里寻找问题的指示。

我正在做的是在模型上运行查找id。 当我在某组记录上运行它时,它们会按预期返回该记录。 但是还有另外100条记录,我可以在数据库(mysql)中看到,但运行find命令会返回ActiveRecord :: RecordNotFound异常。

如果我直接在数据库中创建另一条记录,我也无法提取该记录。 但是,如果我在rails控制台中执行新命令,则该记录是可检索的。 如果我执行find_by_sql命令,则可以检索记录。

我已经尝试过对表格进行优化,但除此之外,我对于导致这种情况的原因感到很难过。 我正在使用Ruby 1.8.7和本地机器上的mysql gem运行Rails 2.3.12。 任何关于可能原因的想法都将不胜感激。 谢谢!

编辑感谢指针 - 将记录器附加到我的控制台实例,这暴露了问题 - 一个被遗忘的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.

最满意答案

模型中必定存在限制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.

更多推荐

本文发布于:2023-07-31 01:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340453.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   发现   find   Rails   exists

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!