HighDots Forums  

new column in ActiveRecord object, method_missing() calledintermittently

Ruby On Rails Talk Ruby On Rails programming language mailing list


Discuss new column in ActiveRecord object, method_missing() calledintermittently in the Ruby On Rails Talk forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
jenna_s
 
Posts: n/a

Default new column in ActiveRecord object, method_missing() calledintermittently - 11-06-2009 , 02:34 AM






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", rder=>"distance asc"}]

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

Location.find(:all,rigin =>[@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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #2  
Old   
Frederick Cheung
 
Posts: n/a

Default Re: new column in ActiveRecord object, method_missing()called intermittently - 11-06-2009 , 03:25 AM






On Nov 6, 7:34*am, jenna_s <jenna.sim... (AT) gmail (DOT) com> wrote:

Quote:
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
Quote:
Thanks,
Jenna
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #3  
Old   
Matt Jones
 
Posts: n/a

Default Re: new column in ActiveRecord object, method_missing()called intermittently - 11-06-2009 , 05:13 PM



On Nov 6, 2:34*am, jenna_s <jenna.sim... (AT) gmail (DOT) com> wrote:
Quote:
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 Jones
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #4  
Old   
mike
 
Posts: n/a

Default Re: new column in ActiveRecord object, method_missing()called intermittently - 11-07-2009 , 12:14 PM



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 <jenna.simmer (AT) gmail (DOT) com>:
Quote:
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", rder=>"distance asc"}]

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

Location.find(:all,rigin =>[@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



--
Von meinen Mobilgerät aus gesendet

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #5  
Old   
jenna_s
 
Posts: n/a

Default Re: new column in ActiveRecord object, method_missing()called intermittently - 11-08-2009 , 11:03 PM



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

On Nov 6, 12:25*am, Frederick Cheung <frederick.che... (AT) gmail (DOT) com>
wrote:
Quote:
On Nov 6, 7:34*am, jenna_s <jenna.sim... (AT) gmail (DOT) com> 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



Thanks,
Jenna
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #6  
Old   
jenna_s
 
Posts: n/a

Default Re: new column in ActiveRecord object, method_missing()called intermittently - 11-08-2009 , 11:04 PM



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

On Nov 6, 2:13*pm, Matt Jones <al2o... (AT) gmail (DOT) com> wrote:
Quote:
On Nov 6, 2:34*am, jenna_s <jenna.sim... (AT) gmail (DOT) com> 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 Jones
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #7  
Old   
jenna_s
 
Posts: n/a

Default Re: new column in ActiveRecord object, method_missing()called intermittently - 11-08-2009 , 11:04 PM



I'll try it. Thanks.
Jenna

On Nov 7, 9:14*am, mike <mikez... (AT) gmail (DOT) com> wrote:
Quote:
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 <jenna.sim... (AT) gmail (DOT) com>:





Hi,

I'm using thegeokitplugin 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.Geokitoverrides 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", rder=>"distance asc"}]

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

Location.find(:all,rigin =>[@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 thegeokitgroup but nobody answered it:
http://groups.google.com/group/geokit/browse_thread/thread/ce364a46d8...
. 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

--
Von meinen Mobilgerät aus gesendet
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #8  
Old   
Marnen Laibow-Koser
 
Posts: n/a

Default Re: new column in ActiveRecord object, method_missing() call - 11-09-2009 , 12:07 AM



jenna_s wrote:
Quote:
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
marnen (AT) marnen (DOT) org
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #9  
Old   
jenna_s
 
Posts: n/a

Default Re: new column in ActiveRecord object, method_missing()called intermittently - 11-14-2009 , 06:55 PM



Hi Mike,

I tried your suggestion (putting sort_by_distance_from right after the
find()) and it seems to work. I don't understand why, because I really
don't have an :include in the find(). I'm just disappointed with
myself because I thought I can debug any Rails problem, and this one
showed me that I can't.

Thanks,
J

On Nov 7, 9:14*am, mike <mikez... (AT) gmail (DOT) com> wrote:
Quote:
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 <jenna.sim... (AT) gmail (DOT) com>:





Hi,

I'm using thegeokitplugin 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.Geokitoverrides 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", rder=>"distance asc"}]

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

Location.find(:all,rigin =>[@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 thegeokitgroup but nobody answered it:
http://groups.google.com/group/geokit/browse_thread/thread/ce364a46d8...
. 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

--
Von meinen Mobilgerät aus gesendet
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.