简体   繁体   English

Rails4:ActiveRecord.find如何将一个具有不同id的对象返回给查询的对象?

[英]Rails4: How could ActiveRecord.find return an object with a different id to the one queried?

I have an issue where ActiveRecord.find is returning incorrect rows, but only in production. 我有一个问题,ActiveRecord.find返回不正确的行,但只在生产中。 Here's some sample code: 这是一些示例代码:

    <% puts group_hash %>
    <% puts Group.find(group_hash['id'].to_i).inspect %>

which outputs: (all attributes other than 'id' have been truncated for readability) 输出:(除了'id'之外的所有属性都被截断以便于阅读)

    {"id"=>550058, ....}
    #<Group id: 265492, ...>

How can Group.find(550058) be yielding a record with the id 265492? Group.find(550058)如何产生id为265492的记录?

I believe a similar issue is happening in a few places. 我相信在一些地方也会发生类似的问题。 It seems to happen when the record has been loaded recently (in the above sample, the group is loaded previously in the controller), so I suspect the cache. 它似乎发生在最近加载记录时(在上面的示例中,该组先前已加载到控制器中),所以我怀疑缓存。

The code has been running fine under Rails3, the moment Rails4 is deployed, this issue happens. 代码在Rails3下运行正常,在Rails4部署的那一刻,就会出现这个问题。 Unfortunately I can't reproduce the issue in any non-production environments (even on a complete clone of production on a separate host, pointing at the production database), so possibly the server load is a contributing factor. 遗憾的是,我无法在任何非生产环境中重现该问题(即使在单独主机上完全克隆生产,指向生产数据库),因此服务器负载可能是一个促成因素。

Any ideas? 有任何想法吗?

I managed to resolve this. 我设法解决了这个问题。 I still have no idea how or why the above could have happened (surely m = Model.find(id); (id == m.id) should always be true!), though I still suspect the cache, as it seemed to only happen if the cache was primed. 我仍然不知道上面是怎么或为什么会发生这种情况(当然m = Model.find(id); (id == m.id)应该永远是真的!),虽然我仍然怀疑缓存,因为它似乎只有在缓存已准备就绪时才会发生。

I did the following, I believe something in these steps resolved the issue: 我做了以下几点,我相信这些步骤中的一些事情解决了这个问题:

  1. pruned my Gemfile removing some unused gems like dalli, and removing the version constraint all but a few cases to allow updating. 修剪我的Gemfile删除一些未使用的宝石,如dalli,并删除版本约束除了少数情况允许更新。
  2. run bundle update resulting in a lot of updated dependancies 运行bundle update导致大量更新的依赖项
  3. I'm was used a custom buildpack on Heroku (forked from the official ruby one), I pulled in the last few months of upstream changes. 我在Heroku上使用了一个自定义构建包(从官方ruby中分离出来),我在最近几个月的上游变化中拉了下来。

Thanks to everyone who read & commented. 感谢所有阅读和评论的人。 I guess something was just amiss in my gem dependancies or heroku environment. 我猜我的宝石依赖或heroku环境中有些不对劲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM