2010-09-25 66 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