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

Ecn*_*lyr 5 ruby-on-rails pg rails-geocoder ruby-on-rails-4

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

@locations = Location.near(params[:search], 50, :order => :distance)
Run Code Online (Sandbox Code Playgroud)

或者,更简洁:

@mymodels = MyModel.near(address, distance, order: :distance)
Run Code Online (Sandbox Code Playgroud)

我收到错误:

PG::UndefinedColumn: ERROR:  column mymodels.distance does not exist
Run Code Online (Sandbox Code Playgroud)

distance列应该由Geocoder gem添加到结果中,但它似乎没有出现在结果中(所以我得到了上面的错误).

Ecn*_*lyr 3

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

@mymodels = MyModel.near(address, distance, order: 'distance')
Run Code Online (Sandbox Code Playgroud)