簡體   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