2010-09-25 62 views
4

我在我的应用程序中遇到了一个小问题。我目前使用geokit来查找给定位置附近的对象,并在找到的集合上使用sort_by_distance_from。使用rails gem geokit按距离和分页排序?

见下文:

@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任何方式进行分页形成DB按距离排序时?

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

+1

我也在为此寻找解决方案。与will_paginate结合会产生不正确的结果。 – benr75 2010-10-05 00:08:38

回答

0

我对解决这个问题,将使用方法:offset和:极限参数查找()

也,有一个geokit车型,距离场:为了=>“距离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) 
+0

距离列不再工作: “在当前版本的geokit-rails中,不可能使用distance列添加where子句,我尝试了许多不同的方法来做到这一点,并没有让它工作。“ 它对于where和order子句的表现方式相同。 https://github.com/geokit/geokit-rails – mauriciomdea 2015-10-20 15:44:49

1

的距离列不工作了。

“在geokit护栏的当前版本,所以无法使用距离栏添加一个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