views:

80

answers:

2

I come across a small issue in my app. I'm currently using geokit to find objects near a given location, and I use the sort_by_distance_from on the found set.

See below:

@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]

Is there any way with geokit to paginate form the DB when sorting by distance?

AKA, not calling the full found set?

+1  A: 

My approach to solve this, would use the :offset and :limit parameters for the find()

also, there is a distance field for geokit models, :order=>'distance asc'

eg.

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)
vrsmn