Rewrite query using heredoc syntax for greater readability

This commit is contained in:
Erik Michaels-Ober 2011-04-03 22:49:23 -07:00
parent 51651ddcf5
commit 511d44875e
1 changed files with 7 additions and 1 deletions

View File

@ -2,7 +2,13 @@ class Hydrant < ActiveRecord::Base
belongs_to :user
def self.find_closest(lat, lng, limit=50)
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])
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
Hydrant.find_by_sql([query, lat, lng, lat, limit])
end
def adopted?