Question

I have an issue where ActiveRecord.find is returning incorrect rows, but only in production. 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"=>550058, ....}
    #<Group id: 265492, ...>

How can Group.find(550058) be yielding a record with the 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. 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?

Was it helpful?

Solution

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.

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.
  2. run bundle update resulting in a lot of updated dependancies
  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.

Thanks to everyone who read & commented. I guess something was just amiss in my gem dependancies or heroku environment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top