@@ -116,14 +116,24 @@ def geokit_finder_adapter
116116 end
117117
118118 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+
119123 options [ :within ] = distance
124+
120125 # Add bounding box to speed up SQL request.
121126 bounds = formulate_bounds_from_distance (
122127 options ,
123128 normalize_point_to_lat_lng ( options [ :origin ] ) ,
124129 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 ) )
127137 end
128138 alias inside within
129139
@@ -154,9 +164,9 @@ def by_distance(options = {})
154164 units = extract_units_from_options ( options )
155165 formula = extract_formula_from_options ( options )
156166 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' ) )
160170 end
161171
162172 def with_latlng
0 commit comments