Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving the error PG::UndefinedColumn: ERROR: column mymodels.distance does not exist when using Geocoder's near method

When using this query (the same as in Railscasts episode #273):

@locations = Location.near(params[:search], 50, :order => :distance)

Or, to be more concise:

@mymodels = MyModel.near(address, distance, order: :distance)

I get the error:

PG::UndefinedColumn: ERROR:  column mymodels.distance does not exist

The distance column is supposed to be added to the results by the Geocoder gem but it does not appear to appear in the results (so I get the above error).

like image 312
Ecnalyr Avatar asked Oct 21 '22 12:10

Ecnalyr


1 Answers

When using Postgres along with the Geocoder gem, your queries cannot use symbolized keys (in this case, :distance) in the query. Using the string 'distance' in this case avoids the problem.

@mymodels = MyModel.near(address, distance, order: 'distance')
like image 107
Ecnalyr Avatar answered Nov 03 '22 17:11

Ecnalyr