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