Module: Sequel::Plugins::PgLocation::DatasetMethods

Defined in:
lib/sequel/plugins/pg_location.rb

Instance Method Summary collapse

Instance Method Details

#nearest(lat, lng, radius) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/sequel/plugins/pg_location.rb', line 23

def nearest(lat,lng,radius)
	location_cache_field = model.location_cache_field
	radius_in_km = (radius.to_i * 1609.3).to_f
	lat = lat.to_f
	lng = lng.to_f
	where("earth_box(ll_to_earth(?,?),?) @> #{location_cache_field}", lat, lng, radius_in_km).where("earth_distance(ll_to_earth(?, ?), #{location_cache_field}) < ?", lat, lng, radius_in_km).select_append{
		(Sequel.function(:earth_distance, Sequel.function(:ll_to_earth,lat,lng), location_cache_field).as('distance'))
		}.order('distance')
end