2011-02-23 20:50:59 +00:00
|
|
|
class Hydrant < ActiveRecord::Base
|
2011-05-22 14:27:36 +00:00
|
|
|
validates_presence_of :lat, :lng
|
2011-02-23 20:50:59 +00:00
|
|
|
belongs_to :user
|
2011-05-22 14:35:46 +00:00
|
|
|
has_many :reminders
|
2011-02-23 20:50:59 +00:00
|
|
|
|
2011-03-17 23:37:15 +00:00
|
|
|
def self.find_closest(lat, lng, limit=50)
|
2011-04-04 05:49:23 +00:00
|
|
|
query = <<-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 ?
|
|
|
|
SQL
|
2011-05-22 13:16:12 +00:00
|
|
|
Hydrant.find_by_sql([query, lat.to_f, lng.to_f, lat.to_f, limit.to_i])
|
2011-02-23 20:50:59 +00:00
|
|
|
end
|
2011-03-17 18:46:06 +00:00
|
|
|
|
|
|
|
def adopted?
|
|
|
|
!user.nil?
|
|
|
|
end
|
|
|
|
|
2011-02-23 20:50:59 +00:00
|
|
|
end
|