# Intermittent problem finding records - ever seen this?

**URL:** <https://www.ruby-forum.com/t/intermittent-problem-finding-records-ever-seen-this/82060>\
**Category:** Rails\
**Created:** [January 17, 2007, 4:35am UTC](https://www.ruby-forum.com/t/intermittent-problem-finding-records-ever-seen-this/82060 "2007-01-17T04:35:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mindel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@mindel](https://www.ruby-forum.com/u/mindel)\
**Post date:** [January 17, 2007, 4:35am UTC](https://www.ruby-forum.com/t/intermittent-problem-finding-records-ever-seen-this/82060/1 "2007-01-17T04:35:12Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![mindel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@mindel](https://www.ruby-forum.com/u/mindel)\
**Post date:** [January 18, 2007, 4:04am UTC](https://www.ruby-forum.com/t/intermittent-problem-finding-records-ever-seen-this/82060/2 "2007-01-18T04:04:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mindel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@mindel](https://www.ruby-forum.com/u/mindel)\
**Post date:** [January 18, 2007, 4:22am UTC](https://www.ruby-forum.com/t/intermittent-problem-finding-records-ever-seen-this/82060/3 "2007-01-18T04:22:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mindel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@mindel](https://www.ruby-forum.com/u/mindel)\
**Post date:** [January 18, 2007, 4:52am UTC](https://www.ruby-forum.com/t/intermittent-problem-finding-records-ever-seen-this/82060/4 "2007-01-18T04:52:43Z")

</div>

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.) 🙂

> > 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.

---

<div class="post-metadata">

**Author:** ![mindel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@mindel](https://www.ruby-forum.com/u/mindel)\
**Post date:** [January 18, 2007, 10:31am UTC](https://www.ruby-forum.com/t/intermittent-problem-finding-records-ever-seen-this/82060/5 "2007-01-18T10:31:58Z")

</div>

On 1/18/07, Ryan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
