接收错误PG :: UndefinedColumn:ERROR:使用Geocoder的near方法时,mymodels.distance列不存在

使用此查询时(与Railscasts第273集中相同):

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

或者,更简洁:

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

我收到错误:

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

应该通过Geocoder gem将distance列添加到结果中,但它似乎不会出现在结果中(因此我得到上述错误)。

将Postgres与Geocoder gem一起使用时,查询中的查询不能使用符号化键(在本例中为:distance )。 在这种情况下使用字符串'distance'可以避免此问题。

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