繁体   English   中英

使用rails gem geokit按距离和分页排序?

[英]Using rails gem geokit sort by distance and pagination?

我在我的应用中遇到了一个小问题。 我目前正在使用geokit查找给定位置附近的对象,并在找到的集合上使用sort_by_distance_from。

见下文:

@find = Item.find(:all, :origin =>[self.geocode.lat.to_f,self.geocode.lng.to_f], :within=>50, :include=>[:programs], :conditions=>["programs.name = ?", self.name])
  @find.sort_by_distance_from([self.geocode.lat.to_f,self.geocode.lng.to_f]

在按距离排序时,是否有任何方法可以使用geokit对数据库进行分页?

AKA,没有调用完整的发现集?

距离列不再有效:

“在当前版本的geokit-rails中,不可能使用距离列添加where子句。我已经尝试了很多不同的方法来做到这一点并且没有让它工作。”

它对于where和order子句的行为方式相同。

人们期望建立这样的查询:

scoped  = Location.geo_scope(:origin => @somewhere)
scoped  = scoped.where('distance <= 5')
results = scoped.all

这是不可能的,它必须在一个步骤中完成,如下所示:

scoped = Location.within(5, :origin => @somewhere)
results = scoped.all

github.com/geokit/geokit-rails

我解决这个问题的方法是使用find()的:offset和:limit参数

此外,geokit模型还有一个距离场:order =>'distance asc'

例如。

page = 0 unless params[:page]
items_per_page = 20
offset = page * items_per_page
@find = Item.find(:all, :origin =>[self.geocode.lat.to_f,self.geocode.lng.to_f], :within=>50, :include=>[:programs], :conditions=>["programs.name = ?", self.name], :order => 'distance asc', :limit => items_per_page, :offset => page)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM