Skip to content

Instantly share code, notes, and snippets.

@danielcooper
Created February 23, 2012 16:55
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 danielcooper/1893730 to your computer and use it in GitHub Desktop.
Save danielcooper/1893730 to your computer and use it in GitHub Desktop.
Kris should have stayed at homeflow
# This gets X users within a distance of '10' radians
user = User.find_me_a_bloody_user
result = ActiveRecord::Base.connection.execute("
SELECT
((ACOS( SIN( #{user.lat} * PI( ) /180 ) * SIN( latitude * PI( ) /180 ) + COS( #{user.lat} * PI( ) /180 ) * COS( latitude * PI( ) /180 ) * COS( (
#{user.lng} - longitude) * PI( ) /180 ) ) *180 / PI( ) ) *60 * 1.1515)
AS `distance` , users
FROM `users`
HAVING `distance` <=10
ORDER BY `distance` ASC
LIMIT 0 , 1"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment