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

Added support for :through associations in XXX.sort_by_distance_from #26

Open vladgurovich opened 13 years ago

vladgurovich commented 13 years ago

I needed to have results include calculated distances, so per your docs I tried to use

bounds=Bounds.from_point_and_radius(home,5) stores=Store.find :all, :include=>[:reviews,:cities] :bounds=>bounds stores.sort_by_distance_from(home)

Where Store has act_as_mappable :through => :location

Unfortunately sort_by distance_from doesnt work with :through associations, but you can fix it with the following In acts_as_mappable.rb in sort_by_distance_from function replace the following line:

e.send("#{distance_attribute_name}=", e.distance_to(origin,opts))

with the following 2 lines:

responder = e.through.blank? ? e : e.send(e.through) e.send("#{distance_attribute_name}=", responder.distance_to(origin,opts))