@@ -116,14 +116,24 @@ def geokit_finder_adapter
116
116
end
117
117
118
118
def within ( distance , options = { } )
119
+ origin = extract_origin_from_options ( options )
120
+ units = extract_units_from_options ( options )
121
+ formula = extract_formula_from_options ( options )
122
+
119
123
options [ :within ] = distance
124
+
120
125
# Add bounding box to speed up SQL request.
121
126
bounds = formulate_bounds_from_distance (
122
127
options ,
123
128
normalize_point_to_lat_lng ( options [ :origin ] ) ,
124
129
options [ :units ] || default_units )
125
- with_latlng . where ( bound_conditions ( bounds ) ) .
126
- where ( distance_conditions ( options ) )
130
+
131
+ distance_column_name = distance_sql ( origin , units , formula )
132
+
133
+ with_latlng
134
+ . select ( "#{ distance_column_name } AS #{ self . distance_column_name } , #{ table_name } .*" )
135
+ . where ( bound_conditions ( bounds ) )
136
+ . where ( distance_conditions ( options ) )
127
137
end
128
138
alias inside within
129
139
@@ -154,9 +164,9 @@ def by_distance(options = {})
154
164
units = extract_units_from_options ( options )
155
165
formula = extract_formula_from_options ( options )
156
166
distance_column_name = distance_sql ( origin , units , formula )
157
- with_latlng . order (
158
- Arel . sql ( distance_column_name ) . send ( options [ :reverse ] ? 'desc' : 'asc' )
159
- )
167
+ with_latlng
168
+ . select ( " #{ distance_column_name } AS #{ self . distance_column_name } , #{ table_name } .*" )
169
+ . order ( Arel . sql ( distance_column_name ) . send ( options [ :reverse ] ? 'desc' : 'asc' ) )
160
170
end
161
171
162
172
def with_latlng
0 commit comments