geokit / geokit-rails

Official Geokit plugin for Rails/ActiveRecord. Provides location-based goodness for your Rails app. Requires the Geokit gem.
MIT License
1.57k stars 245 forks source link

Mysql::Error: Unknown column 'distance' #47

Closed nhinze closed 10 years ago

nhinze commented 10 years ago

I'm using Rails 3.2.15, ruby 1.8.7 with geokit-rails 2.0. I get the following error message on a by_distance query:

Mysql::Error: Unknown column 'distance' in 'order clause': SELECT tests.* FROM tests WHERE (course_type = 1 OR course_type = 2) AND ((sharemode = 1 OR sharemode = 3) AND event_date >= '2011-01-01 00:00:00' AND event_date <= '2014-01-01 00:00:00') ORDER BY (ACOS(least(1,COS(0.0)_COS(0.0)_COS(RADIANS(tests.latitude))_COS(RADIANS(tests.longitude))+ COS(0.0)_SIN(0.0)_COS(RADIANS(tests.latitude))_SIN(RADIANS(tests.longitude))+ SIN(0.0)_SIN(RADIANS(tests.latitude))))_3963.19) asc, country, distance ASC

Query:

@test_results = Test.app_mode(request.domain).by_distance(:origin => [0,0]).find(:all, :conditions => ["(sharemode = ? OR sharemode = ?) AND event_date >= ? AND event_date <= ?", 1, 3, @search_window_past, @search_window_future], :order => 'country, distance ASC')

Thanks,

Nick,

mnoack commented 10 years ago

That function has been removed as overriding core ruby classes is a bad practice.

See https://github.com/geokit/geokit-rails/issues/39 for alternate solutions

nhinze commented 10 years ago

Ok. Is it possible to have this gem calculate distances as a 2nd step?

Also, this should probably be removed from the "MAKE A MODEL MAPPABLE":

The plug-in creates a calculated distance field on AR instances that have been retrieved through a Geokit location query. By default, these fields are known as "distance" but this can be changed through the :distance_field_name key.