Mysql2::Error:... when calling `near` in geocoder gem
Mysql2::Error:... when calling `near` in geocoder gem
所以我有一个服务列表,所有服务都使用 geogoder gem 进行了地理编码。
调用Service.near("London")
抛出
ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in
your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near
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
工作没有问题。
谁能告诉我做错了什么?
提前致谢
您必须发送正确的参数,以便 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 为我的一个模型生成的。我正在传递地理坐标并测试 "active" 标志:
[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
在某些时候我显然添加然后删除了 Geokit gem。
留在模型中的是 acts_as_mappable
这导致 SQL 语法被抛出。
所以我有一个服务列表,所有服务都使用 geogoder gem 进行了地理编码。
调用Service.near("London")
抛出
ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
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
工作没有问题。
谁能告诉我做错了什么?
提前致谢
您必须发送正确的参数,以便 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 为我的一个模型生成的。我正在传递地理坐标并测试 "active" 标志:
[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
在某些时候我显然添加然后删除了 Geokit gem。
留在模型中的是 acts_as_mappable
这导致 SQL 语法被抛出。