Mysql2 ::错误:...在地理编码器gem中调用`near`时

时间:2016-05-05 19:26:06

标签: mysql ruby-on-rails-4 rails-geocoder

所以我有一份服务列表,全部用geogoder gem进行地理编码。

调用Service.near("London")投掷

  

ActiveRecord :: StatementInvalid:Mysql2 :: Error:你有错误       你的SQL语法;查看与MySQL对应的手册       服务器版本,用于在

附近使用正确的语法

Service.near("London").to_sql

给出

SELECT services.*, MOD(CAST((ATAN2( ((services.longitude - -4.8824474) / 57.2957795), 
      ((services.latitude - 36.510071) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing 
FROM `services` 
WHERE (services.latitude BETWEEN 36.220607433778305 AND 36.7995345662217 
    AND services.longitude BETWEEN -5.242587667147616 AND -4.522307132852385 
    AND () BETWEEN 0.0 AND 20)  
ORDER BY distance ASC

distance_between无问题。

有谁能告诉我做错了什么?

提前致谢

2 个答案:

答案 0 :(得分:0)

您必须发送正确的参数才能使Geocoder gem合成工作SQL。

gem documentation中,虽然没有明确说明,但您需要完全指定所有参数

nearbys = Place.near("Omaha, NE", 50,:order => "distance")

一般地

   nearbys = Model.near("Gecodeable address string", N (range constant), order: parameter)

对于您的情况,我建议您尝试使用以下内容查看是否会让您超越MySQL错误

@result = Service.near("London, UK", YOUR_DISTANCE_CONSTANT, order: false)

我不知道这有多大帮助,但这是我为其中一个模型生成的SQL .near。我正在通过地理坐标并测试一个"活跃的" flag:

[1m[36mAnimalRescue Load (0.0ms)[0m [1mSELECT animal_rescues.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((30.326374 - animal_rescues.latitude) * PI() / 180 / 2), 2) + COS(30.326374 * PI() / 180) * COS(animal_rescues.latitude * PI() / 180) * POWER(SIN((-97.771258 - animal_rescues.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((animal_rescues.longitude - -97.771258) / 57.2957795), ((animal_rescues.latitude - 30.326374) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "animal_rescues" WHERE "animal_rescues"."active_listing" = 't' AND (animal_rescues.latitude BETWEEN 29.77639322417878 AND 30.876354775821223 AND animal_rescues.longitude BETWEEN -98.40842692015944 AND -97.13408907984056 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((30.326374 - animal_rescues.latitude) * PI() / 180 / 2), 2) + COS(30.326374 * PI() / 180) * COS(animal_rescues.latitude * PI() / 180) * POWER(SIN((-97.771258 - animal_rescues.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 38) ORDER BY delivery_item_count + dropoff_item_count DESC LIMIT 3[0m

答案 1 :(得分:0)

在某些时候,我显然已添加,然后删除了Geokit gem。

模型中的左侧是acts_as_mappable

这导致SQL语法被抛弃。