Intermittent problem finding records - ever seen this?

I’m pulling my hair out. And I don’t have that much to start with. If
anyone can give me any ideas or pointers, I’d REALLY appreciate it.

My rails app is intermittently having problems finding records of 2 of
my models. I’m on Rails 1.1.6, ruby 1.8.5, Apache 2.0.52 and Postgres
8.1.4 on Redhat Linux hosted at Rimu.

  1. I sometimes get “Couldn’t find Address with ID=4”. This problem has
    happened in a couple different places in my code, where I’m finding
    Addresses. It usually happens on the same address record (there are 2
    that it has trouble finding more often than others). I can’t find
    anything odd about those addresses.

My Address model is pretty straightforward:

class Address < ActiveRecord::Base
validates_numericality_of :zip, :only_integer => true
validates_presence_of :street_1, :city, :state, :zip
belongs_to :user
has_many :availabilities

Plus a few methods, but no callbacks.

end

  1. In another case, it can’t find records of my Zipcode model. I
    think I eliminated the problem in one spot by adding logging
    statements – how strange is that? This code caused the error pretty
    consistently:

     pickup_address = Address.find(item.pickup_location)
     item_zip = Zipcode.find_by_zipcode(pickup_address.zip)
     #item_zip ended up being nil sometimes
    

This somehow caused the error to stop happening in this part of the code
at least. Note the added log statements.

pickup_address = Address.find(item.pickup_location)
logger.error(“Found pickup_address: #{pickup_address.id}”)

    item_zip = Zipcode.find_by_zipcode(pickup_address.zip)
    logger.error("item_zip: #{item_zip}")

Any ideas?? Ever seen a similar problem?

  • Ryan

Well, still haven’t figured out the problem, but I think I found a way
to workaround it. In the main place in my code where it can’t find
Address objects of Items, I used a begin/rescue that retries a few
times. I see in the logs that it is still happening, but it finds the
records successfully on retry. Would still love to know what’s going
on, though.

class Item < ActiveRecord::Base
def pickup_address
retries = 0
begin
retries += 1
address = Address.find(pickup_location)
rescue
logger.error(“ERROR in item.pickup_address … retry #
#{retries}”)
retry unless retries > 5
end
end
end

I seem to remember this from a very long time ago. I believe that
what’s
happening is that in this line

pickup_address = Address.find(item.pickup_location)

item.pickup_location is nil. and nil.id == 4
If you change it to

pickup_address = Address.find( :first, item.pickup_location )

Out of curiosity, what is item.pickup_location? Is it an address?

If it is, just use
pickup_address = item.pickup_location

anyway. Hopefully that’s on the right track.

Hi Daniel, thanks for the response. None of my Items have a missing
pickup_address (it just happens to error on address #4 a lot, but also
#5 and #26.) :slight_smile:

Out of curiosity, what is item.pickup_location? Is it an address?

item.pickup_location is an address.id.
This was my first Rails app and I when I started writing it, I never set
up has_many / belongs_to relationship between Item and Address. Thought
about trying that here to see if it would fix things, but I really
wanted to figure out the root problem to what I’m seeing. It’s really
puzzling me!

I’ve combed the forum archives but haven’t seen anything similar.

On 1/18/07, Ryan C. [email protected] wrote:

about trying that here to see if it would fix things, but I really
wanted to figure out the root problem to what I’m seeing. It’s really
puzzling me!

I’ve combed the forum archives but haven’t seen anything similar.

Hmm… I’m guessing:
#1 – There’s a bug in your app.

Dig out the query from an exception trace or your logs, ensure sure it
makes sense and returns what you expect when run from psql.

#2 – Hardware issues

Likely to cause intermittent errors. Should be plenty of resources for
diagnosing this sort of thing on google, I think.

Perhaps you could write something automated that repeatedly dumps the
db (or a copy if you can’t take it offline), then compress and finally
md5sum the output.

Try reinstalling/upgrading pg if the hash changes while your data
doesn’t, then talk to the hosting co when there are still issues.

#3 – Something weird in your rails/ruby/ruby<->postgres
driver/postgres install or configuration.

Ouch. No idea…

Good luck,
Isak