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

Missing 'distance' field on returned instances #39

Closed xnzac closed 10 years ago

xnzac commented 11 years ago

As documented:

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

"distance" field on returned objects is not available.

xnzac commented 11 years ago

Not sure how to add this to a github message, but I resolved the issue by reversing the regressive commit: https://github.com/zaczheng/geokit-rails/commit/15dd4b6d1615c77563438bd968e3912f455b1adc

And then 'distance' is back!

dougc84 commented 11 years ago

+1 on this. @zaczheng 's fix works - can you do a PR on this?

xnzac commented 11 years ago

The repo owner wanted to extract the distance calculation out. See the following commit:

https://github.com/geokit/geokit-rails/commit/f79bbdfb81d2a87c24c152cbfbf1638f2eacd878 "I've pushed a new commit with a helper method and some tests and removes the Array sort_by_distance_from method alltogether."

mnoack commented 11 years ago

I'm open to PRs but overriding core classes is best avoided for specific uses.

mnoack commented 11 years ago

Yes, so the new way of doing it (see test in commit referenced above) is: unsorted_collection = DistanceCollection.new(unsorted) unsorted_collection.set_distance_from(locations(:a)) unsorted_collection.sort_by(&:distance)

I would happy accept something like:

class DistanceCollection
  def sort_by_distance_from(origin, opts = {})
    set_distance_from(origin)
    sort_by(&:distance)
  end
end

This would enable a shorter usage like:

DistanceCollection.new(unsorted).sort_by_distance_from(origin)
speakingcode commented 7 years ago

Is there no way to get the actual distance? I don't want to sort by distance i want to display the distance to my user, I need the actual field... how to get the distance field?

hcyildirim commented 6 years ago

@speakingcode did you figured it out? i need help 😞 @mnoack

BilalAamirr commented 5 years ago

Did anyone figured it out? @ccoeder ?

hcyildirim commented 5 years ago

You can look at this issue, there is patch on that https://github.com/geokit/geokit-rails/issues/56