Ruby on Rails中的Geokit,距离查找器,如何按离最近的距离进行排序?

时间:2010-12-15 05:59:28

标签: ruby-on-rails geokit

我正在使用Geokit。我的模型中有以下内容:

  # 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

然后在我的控制器中:

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

目前输出以ID升序列出。我希望它从最近到最远排序。我该怎么办?

感谢。

1 个答案:

答案 0 :(得分:0)

我相信你要找的是:

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

或者说文档:http://geokit.rubyforge.org/

相关问题