From 19a8c8f204c82c0775f6e7bfe98998a91e404d99 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Fri, 28 Sep 2012 22:57:21 -0400
Subject: [PATCH] Always include distance and bearing columns.

This addresses issue #199 and is more directly a response to the
excellent comments from github.com/zetetic on commit 1b65830.
---
 lib/geocoder/stores/active_record.rb | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/lib/geocoder/stores/active_record.rb b/lib/geocoder/stores/active_record.rb
index 805ee6a4..7fd47a5a 100644
--- a/lib/geocoder/stores/active_record.rb
+++ b/lib/geocoder/stores/active_record.rb
@@ -36,7 +36,10 @@ module Geocoder::Store
           if Geocoder::Calculations.coordinates_present?(latitude, longitude)
             near_scope_options(latitude, longitude, *args)
           else
-            where(false_condition) # no results if no lat/lon given
+            # If no lat/lon given we don't want any results, but we still
+            # need distance and bearing columns so you can add, for example:
+            # .order("distance")
+            select(select_clause(nil, "NULL", "NULL")).where(false_condition)
           end
         }
 
@@ -49,7 +52,9 @@ module Geocoder::Store
         #
         scope :within_bounding_box, lambda{ |bounds|
           sw_lat, sw_lng, ne_lat, ne_lng = bounds.flatten if bounds
-          return where(false_condition) unless sw_lat && sw_lng && ne_lat && ne_lng
+          unless sw_lat && sw_lng && ne_lat && ne_lng
+            return select(select_clause(nil, "NULL", "NULL")).where(false_condition)
+          end
           spans = "#{geocoder_options[:latitude]} BETWEEN #{sw_lat} AND #{ne_lat} AND "
           spans << if sw_lng > ne_lng   # Handle a box that spans 180
             "#{geocoder_options[:longitude]} BETWEEN #{sw_lng} AND 180 OR " +
-- 
GitLab