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

懵懂的女人 提交于 2019-12-23 11:44:07

问题


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).


回答1:


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')



回答2:


I could solve this (with rails 5.2.2) using:

@mymodels = Mymodel.where(publish: true).near(params[:search], 50, units: :km)


来源:https://stackoverflow.com/questions/22455937/receiving-the-error-pgundefinedcolumn-error-column-mymodels-distance-does-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!