Skip to content

Instantly share code, notes, and snippets.

@thomasv314
Created July 1, 2011 17:00
Show Gist options
  • Save thomasv314/1058944 to your computer and use it in GitHub Desktop.
Save thomasv314/1058944 to your computer and use it in GitHub Desktop.
NoMethodError: Undefined method 'deg2rad' for #<Class:0x0000>
class Listing < ActiveRecord::Base
set_table_name :Listing
scope :near, lambda{
|*args|
origin = *args.first[:origin]
if (origin).is_a?(Array)
origin_lat, origin_lng = origin
else
origin_lat, origin_lng = origin.latitude, origin.longitude
end
origin_lat, origin_lng = deg2rad(origin_lat), deg2rad(origin_lng)
within = *args.first[:within]
{
:conditions => %(
(ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(Listing.lat))*COS(RADIANS(Listing.lng))+
COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(Listing.lat))*SIN(RADIANS(Listing.lng))+
SIN(#{origin_lat})*SIN(RADIANS(Listing.lat)))*3963) <= #{within}
),
:select => %( Listing.*,
(ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(Listing.lat))*COS(RADIANS(Listing.lng))+
COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(Listing.lat))*SIN(RADIANS(Listing.lng))+
SIN(#{origin_lat})*SIN(RADIANS(Listing.lat)))*3963) AS distance
)
}
}
protected
def deg2rad(degrees)
degrees.to_f / 180.0 * Math::PI
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment