Ruby Geocoder Mysql2语法错误

时间:2011-11-17 13:55:01

标签: ruby-on-rails ruby-on-rails-3 mysql2

使用Rails 3.1。我正在使用Ruby地理编码器(不是geokit),will_paginate和Mysql2宝石。以下是我的源代码:

class ShopsController < ApplicationController
  def nearby
    @shop = Shop.find(params[:id])
    @nearby_shops = @shop.nearbys(20).paginate(:page => params[:page], :per_page => 20)
  end
  ...
end

# nearby.html.erb
<% @nearby_shops.each do |nearby_shop| %>
<p><%= nearby_shop.name %></p>
<% end %>
<%= will_paginate @nearby_shops %>

我在<% @nearby_shops.each do |nearby_shop| %>上遇到了以下Mysql语法错误:

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((48.858334 - lat) * PI() / 180 / 2),' at line 1: SELECT COUNT(*) AS count_all, *, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((48.858334 - lat) * PI() / 180 / 2), 2) + COS(48.858334 * PI() / 180) * COS(lat * PI() / 180) * POWER(SIN((2.294254 - lng) * PI() / 180 / 2), 2) )) AS distance, CAST(DEGREES(ATAN2( RADIANS(lng - 2.294254), RADIANS(lat - 48.858334))) + 360 AS decimal) % 360 AS bearing, shops.id,shops.shop_type,shops.name,shops.shop_subtype,shops.operation_hours,shops.check_in,shops.check_out,shops.season,shops.prices,shops.country,shops.postal_code,shops.administrative_area_level_1,shops.administrative_area_level_2,shops.formatted_address,shops.contact,shops.email,shops.web,shops.description,shops.reviews_count,shops.rating_average,shops.lat,shops.lng,shops.created_at,shops.updated_at,shops.photos_count,shops.provider,shops.provider_price,shops.provider_ref,shops.continent,shops.directions,shops.street_address,shops.route,shops.intersection,shops.political,shops.administrative_area_level_3,shops.colloquial_area,shops.locality,shops.sublocality,shops.neighborhood,shops.premise,shops.subpremise,shops.natural_feature,shops.airport,shops.park,shops.post_box,shops.street_number,shops.floor,shops.room AS shops_id_shops_shop_type_shops_name_shops_shop_subtype_shops_operation_hours_shops_check_in_shops_check_out_shops_season_shops_prices_shops_country_shops_postal_code_shops_administrative_area_level_1_shops_administrative_area_level_2_shops_formatted_addre FROM `shops`  WHERE (lat BETWEEN 48.5688704337783 AND 49.1477975662217 AND lng BETWEEN 1.8542883270272932 AND 2.734219672972707 AND shops.id != 1) GROUP BY shops.id,shops.shop_type,shops.name,shops.shop_subtype,shops.operation_hours,shops.check_in,shops.check_out,shops.season,shops.prices,shops.country,shops.postal_code,shops.administrative_area_level_1,shops.administrative_area_level_2,shops.formatted_address,shops.contact,shops.email,shops.web,shops.description,shops.reviews_count,shops.rating_average,shops.lat,shops.lng,shops.created_at,shops.updated_at,shops.photos_count,shops.provider,shops.provider_price,shops.provider_ref,shops.continent,shops.directions,shops.street_address,shops.route,shops.intersection,shops.political,shops.administrative_area_level_3,shops.colloquial_area,shops.locality,shops.sublocality,shops.neighborhood,shops.premise,shops.subpremise,shops.natural_feature,shops.airport,shops.park,shops.post_box,shops.street_number,shops.floor,shops.room HAVING 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((48.858334 - lat) * PI() / 180 / 2), 2) + COS(48.858334 * PI() / 180) * COS(lat * PI() / 180) * POWER(SIN((2.294254 - lng) * PI() / 180 / 2), 2) )) <= 20

如果我删除了分页,则不会出现任何问题,但列表当然是完整的项目列表。

知道可以采取哪些措施来解决这个问题?感谢。

1 个答案:

答案 0 :(得分:2)

原来是ActiveRecord / Geocoder中的一个错误,我将这个分支用于我的Ruby地理编码器gem来解决所有问题:

gem 'geocoder', :git => 'git://github.com/alexreisner/geocoder.git', :branch => 'no_grouping'

按照此处的讨论:

https://github.com/alexreisner/geocoder/issues/86