# New column in ActiveRecord object, method\_missing() called intermittently

**URL:** <https://www.ruby-forum.com/t/new-column-in-activerecord-object-method-missing-called-intermittently/178545>\
**Category:** Rails\
**Created:** [November 6, 2009, 9:14am UTC](https://www.ruby-forum.com/t/new-column-in-activerecord-object-method-missing-called-intermittently/178545 "2009-11-06T09:14:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenna\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a4c791/32.png) [@jenna\_s](https://www.ruby-forum.com/u/jenna_s)\
**Post date:** [November 6, 2009, 9:14am UTC](https://www.ruby-forum.com/t/new-column-in-activerecord-object-method-missing-called-intermittently/178545/1 "2009-11-06T09:14:18Z")

</div>

Hi,

I’m using the geokit plugin to provide a “find-locations-on-radius”  
feature in my app. When a location is added to the database, the  
actual address is translated (geocoded) into a longitude & latitude.  
The longitude and the latitude are stored in the database. When a user  
performs a “find-locations-on-a-X-miles-radius” from a point of origin  
(an address), a distance is calculated from the given point of origin  
to all the stored locations. Geokit overrides the ActiveRecord find  
method to include a distance column:

[:all, {:select=\>“_, SQRT(POW(XXX_(XXX-  
locations.lat),2)+\n POW(XXX\*(XXX-locations.lng),2))  
\n AS distance”, :order=\>“distance asc”}]

Then from any view you can access the distance on the objects  
retrieved by find:

Location.find(:all,:origin =\>[@ip\_location.lat,  
@ip\_location.lng]).each { |location|  
location.distance  
}

The problem that I have is that location.distance returns empty/nil  
once in a while. From what I understand, when location.distance is  
called, it invokes method\_missing() in activerecord/lib/active\_record/  
attribute\_methods.rb. That is because there is no distance column in  
the locations table, so it goes to the object’s attributes to find the  
distance. Well, it just so happens that once in a while method\_missing  
() is not invoked when accessing location.distance. I put tracing in  
method\_missing() and when I get distance values method\_missing was  
indeed called, and when I get blank/nil - method\_missing was not  
called.

I thought it was because of caching, but I turned off caching in  
production, and I don’t have any explicit caching logic in my views or  
in my controllers.

I asked this question on the geokit group but nobody answered it:  
[http://groups.google.com/group/geokit/browse\_thread/thread/ce364a46d802d399](http://groups.google.com/group/geokit/browse_thread/thread/ce364a46d802d399)  
. You can see all debugging and tracing here.

Why doesn’t method\_missing get invoked all the time when  
location.distance is called? What gets invoked then? Could this be  
some sort of caching bug?

Thanks,  
Jenna

---

<div class="post-metadata">

**Author:** ![jenna\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a4c791/32.png) [@jenna\_s](https://www.ruby-forum.com/u/jenna_s)\
**Post date:** [November 6, 2009, 9:37am UTC](https://www.ruby-forum.com/t/new-column-in-activerecord-object-method-missing-called-intermittently/178545/2 "2009-11-06T09:37:43Z")

</div>

On Nov 6, 7:34 am, jenna\_s [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why doesn’t method\_missing get invoked all the time when  
> location.distance is called? What gets invoked then? Could this be  
> some sort of caching bug?

So the times that it returns nil, what does get called ? have you  
tried stepping through it with the debugger ?

Fred

---

<div class="post-metadata">

**Author:** ![jenna\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a4c791/32.png) [@jenna\_s](https://www.ruby-forum.com/u/jenna_s)\
**Post date:** [November 7, 2009, 6:16pm UTC](https://www.ruby-forum.com/t/new-column-in-activerecord-object-method-missing-called-intermittently/178545/3 "2009-11-07T18:16:05Z")

</div>

It returns nil when you use joins or includes on your finds. Try  
records.sort\_by\_distance\_from origin. That will add the distance  
column back

2009/11/6, jenna\_s [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> method to include a distance column:  
> location.distance  
> indeed called, and when I get blank/nil - method\_missing was not  
> Why doesn’t method\_missing get invoked all the time when  
> location.distance is called? What gets invoked then? Could this be  
> some sort of caching bug?
> 
> Thanks,  
> Jenna
> 
> >

–  
Von meinen Mobilgerät aus gesendet

---

<div class="post-metadata">

**Author:** ![jenna\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a4c791/32.png) [@jenna\_s](https://www.ruby-forum.com/u/jenna_s)\
**Post date:** [November 9, 2009, 5:05am UTC](https://www.ruby-forum.com/t/new-column-in-activerecord-object-method-missing-called-intermittently/178545/4 "2009-11-09T05:05:32Z")

</div>

I’ll try that. It’s just that it is hard to make it repeatable for  
debugging.

On Nov 6, 12:25 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jenna\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a4c791/32.png) [@jenna\_s](https://www.ruby-forum.com/u/jenna_s)\
**Post date:** [November 9, 2009, 5:06am UTC](https://www.ruby-forum.com/t/new-column-in-activerecord-object-method-missing-called-intermittently/178545/5 "2009-11-09T05:06:07Z")

</div>

I did read that in the geokit README, but I have no include in my  
statement.

---

<div class="post-metadata">

**Author:** ![jenna\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a4c791/32.png) [@jenna\_s](https://www.ruby-forum.com/u/jenna_s)\
**Post date:** [November 6, 2009, 11:14pm UTC](https://www.ruby-forum.com/t/new-column-in-activerecord-object-method-missing-called-intermittently/178545/6 "2009-11-06T23:14:18Z")

</div>

On Nov 6, 2:34 am, jenna\_s [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why doesn’t method\_missing get invoked all the time when  
> location.distance is called? What gets invoked then? Could this be  
> some sort of caching bug?

The other issue I’ve seen with things like this was that some find  
options (:include, notably) will drop the :select clause, meaning that  
you won’t get an attribute back for distance. Any chance that could be  
involved?

–Matt J.

---

<div class="post-metadata">

**Author:** ![jenna\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a4c791/32.png) [@jenna\_s](https://www.ruby-forum.com/u/jenna_s)\
**Post date:** [November 9, 2009, 5:06am UTC](https://www.ruby-forum.com/t/new-column-in-activerecord-object-method-missing-called-intermittently/178545/7 "2009-11-09T05:06:07Z")

</div>

I’ll try it. Thanks.  
Jenna

---

<div class="post-metadata">

**Author:** ![jenna\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a4c791/32.png) [@jenna\_s](https://www.ruby-forum.com/u/jenna_s)\
**Post date:** [November 9, 2009, 6:07am UTC](https://www.ruby-forum.com/t/new-column-in-activerecord-object-method-missing-called-intermittently/178545/8 "2009-11-09T06:07:38Z")

</div>

jenna\_s wrote:

> I’ll try it. Thanks.  
> Jenna

Please quote when you reply – otherwise it’s impossible to know what  
you’re replying to.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
