Skip to content

Instantly share code, notes, and snippets.

@tudorpavel
Created September 13, 2015 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tudorpavel/7f195011512d4efeeed7 to your computer and use it in GitHub Desktop.
Save tudorpavel/7f195011512d4efeeed7 to your computer and use it in GitHub Desktop.
Rails PostGIS within_range query.
def self.within_range(range_km, lat, lng)
if lat.present? && lng.present?
joins(location: :point)
.where(
"ST_DWithin(Geography(ST_MakePoint(longitude, latitude)),
Geography(ST_MakePoint(#{lng}, #{lat})),
#{(range_km * 1000).to_i})"
)
else
none
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment