重现的步骤:
class Venue
field :coordinates, type: Array
index({coordinates: '2d'}, {min: -180, max: 180})
end
跑了rake db:mongoid:create_indexes
试着打电话:
Venue.geo_near([@lat, @long])
得到了:
Moped::Errors::OperationFailure:
The operation: #<Moped::Protocol::Command
@length=135
@request_id=3
@response_to=0
@op_code=2004
@flags=[]
@full_collection_name="encore_backend_test.$cmd"
@skip=0
@limit=-1
@selector={:geoNear=>"venues", :near=>[43.670906, -79.393331], :query=>{}, :spherical=>true}
@fields=nil>
failed with error "no geo index :("
有什么建议吗?
答案 0 :(得分:14)
愚蠢的错误,忘记用db:mongoid:create_indexes
为RSpec执行RAILS_ENV=test
完成命令
bundle exec rake db:mongoid:create_indexes RAILS_ENV=test