开发者

Geokit in Ruby on Rails, distance finder, how to sort by nearest to farthest?

开发者 https://www.devze.com 2023-01-31 04:35 出处:网络
I\'m using Geokit. I have the following in my model: # Distance-based finder method # Usage: # - find_this_within(Shop.first, 10)

I'm using Geokit. I have the following in my model:

  # Distance-based finder method
  # Usage:
  # - find_this_within(Shop.first, 10)
  def self.find_this_within(origin, within)
    if origin.geocoded?
      find(:all, :origin => origin, :within => within )
    else
      []
    end
  end

Then in my controller:

@shops = s.paginate(:page => params[:page], :per_page => 20)

Currently the output is listed in ID ascending. I want it to sor开发者_C百科t from nearest to furthest. What should I do?

Thanks.


I believe what you are looking for is:

   def self.find_this_within(origin, within)
    if origin.geocoded?
      find(:all, :origin => origin, :within => within, :order => 'distance asc')
    else
      []
    end
  end

Or so say the Docs: http://geokit.rubyforge.org/

0

精彩评论

暂无评论...
验证码 换一张
取 消