【发布时间】:2015-06-03 15:20:03
【问题描述】:
我目前有一个使用地理编码器 gem 的位置数组:
@locations = Location.near([@lat, @lon], 3)
产生:
=> [#<Location id: 18, address: "555 N 7th Ave, Jamaica, CA 66666", phone: "(623) 555-5555", latitude: 66.666, longitude: -200.082, user_id: 1, created_at: "2015-03-11 15:06:57", updated_at: "2015-03-11 15:06:57">,
#<Location id: 46, address: "555 N 7th Ave, Jamaica, CA 66666", phone: "(623) 555-5555", latitude: 66.666, longitude: -200.082, user_id: 1, created_at: "2015-03-11 15:06:57", updated_at: "2015-03-11 15:06:57">]
我正在尝试使用
为这些结果添加分页@locations = Location.near([@lat, @lon], 3).paginate(:page => params[:page], :per_page => 5)
在视图中(haml):
= will_paginate @locations
但我收到以下错误:
ActionView::Template::Error (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 '*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((33.457341299999996 - locations.lati' at line 1: SELECT COUNT(locations.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((33.457341299999996 - locations.latitude) * PI() / 180 / 2), 2) + COS(33.457341299999996 * PI() / 180) * COS(locations.latitude * PI() / 180) * POWER(SIN((-112.07359500000001 - locations.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((locations.longitude - -112.07359500000001) / 57.2957795), ((locations.latitude - 33.457341299999996) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing) FROM `locations` WHERE (locations.latitude BETWEEN 33.41392176506674 AND 33.50076083493325 AND locations.longitude BETWEEN -112.12563828858949 AND -112.02155171141054 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((33.457341299999996 - locations.latitude) * PI() / 180 / 2), 2) + COS(33.457341299999996 * PI() / 180) * COS(locations.latitude * PI() / 180) * POWER(SIN((-112.07359500000001 - locations.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 3)):
有什么想法吗?
【问题讨论】:
-
我现在也看到分页自然不适用于普通数组(但分页集合),仍然不明白sql错误。
-
同样的代码非常适合我。
标签: ruby-on-rails pagination rails-geocoder