adopt-a-hydrant/app/models/hydrant.rb

8 lines
345 B
Ruby
Raw Normal View History

2011-02-23 20:50:59 +00:00
class Hydrant < ActiveRecord::Base
belongs_to :user
2011-02-25 21:51:56 +00:00
def self.find_closest(lat, lng, limit=100)
2011-02-23 20:50:59 +00:00
Hydrant.find_by_sql(["SELECT *, (3959 * ACOS(COS(RADIANS(?)) * COS(RADIANS(lat)) * COS(radians(lng) - RADIANS(?)) + SIN(RADIANS(?)) * SIN(RADIANS(lat)))) AS distance FROM hydrants ORDER BY distance LIMIT ?", lat, lng, lat, limit])
end
end