Rails-Postgres上的gmaps4rails gem [英] Rails - gmaps4rails gem on postgres

查看:65
本文介绍了Rails-Postgres上的gmaps4rails gem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地MySQL机器上成功使用了gmaps4rails gem.但是,当我在Heroku上部署到PG时,对于使用gmaps4rails"near"函数在所选位置附近找到位置的代码,我收到以下错误:

I successfully use the gmaps4rails gem on my local MySQL machine. However, when I deploy to PG on Heroku, I get the following error with respect to code that uses the gmaps4rails "near" function to find locations near the selected location:

2012-05-21T17:58:40+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::Error: ERROR:  operator does not exist: numeric - character varying
2012-05-21T17:58:40+00:00 app[web.1]:                                                              ^
2012-05-21T17:58:40+00:00 app[web.1]: LINE 1: ...8.755864232 * 2 * ASIN(SQRT(POWER(SIN((30.1926300 - venues.l...
2012-05-21T17:58:40+00:00 app[web.1]: HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
2012-05-21T17:58:40+00:00 app[web.1]: : SELECT  venues.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((30.1926300 - venues.latitude) * PI() / 180 / 2), 2) + COS(30.1926300 * PI() / 180) * COS(venues.latitude * PI() / 180) * POWER(SIN((-85.8356740 - venues.longitude) * PI() / 180 / 2), 2) )) AS distance, CAST(DEGREES(ATAN2( RADIANS(longitude - -85.8356740), RADIANS(latitude - 30.1926300))) + 360 AS decimal) % 360 AS bearing FROM "venues"  WHERE (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((30.1926300 - venues.latitude) * PI() / 180 / 2), 2) + COS(30.1926300 * PI() / 180) * COS(venues.latitude * PI() / 180) * POWER(SIN((-85.8356740 - venues.longitude) * PI() / 180 / 2), 2) )) <= 5) ORDER BY distance LIMIT 5):
2012-05-21T17:58:40+00:00 app[web.1]:   app/controllers/venues_controller.rb:22:in `show'

我怀疑这是因为postgres的此查询中不支持某些功能,但是gem可能支持postgres.知道发生了什么事吗?

I suspect that this is because of something not being supported in this query in postgres, but the gem supposedly supports postgres. Any idea what's going on?

推荐答案

好像PostgreSQL在抱怨这一点:

Looks like PostgreSQL is complaining about this:

30.1926300 - venues.latitude

,并且错误消息指出没有允许您从数字中减去字符串的运算符.我猜想您应该将venues.latitude列创建为:string,而该列应该是

and the error message says that there is no operator that allows you to subtract a string from a number. I'd guess that you've created your venues.latitude column as a :string when it should be a :float or :decimal. MySQL tries to be friendly be doing a lot of implicit type conversions behind your back, PostgreSQL tries to be friendly by making you say exactly what you mean to avoid confusion.

您将不得不将latitude列更改为数字类型.然后,如果要在Heroku的PostgreSQL之上进行部署,则应该在PostgreSQL之上开始开发,并且还应该在开发和部署环境中匹配PostgreSQL版本.

You're going to have to change your latitude column to a numeric type. Then you should start developing on top of PostgreSQL if you're going to deploy on top of Heroku's PostgreSQL, you should also match the PostgreSQL version in your development and deployment environments.

AFAIK,您必须使用 ALTER手动更改类型TABLE 作为迁移中的简单change_column可能会失败,并显示与

AFAIK, you'll have to change the type manually with an ALTER TABLE as a simple change_column in a migration will probably fail with an error similar to

列纬度"不能转换为双精度类型

column "latitude" cannot be cast to type double precision

这样的迁移:

def up
    connection.execute(%q{
        alter table venues
        alter column latitude
        type float using latitude::float
    })
end

应该为PostgreSQL解决这个问题.大概还必须修复venues.longitude.

should do the trick for PostgreSQL. Presumably you'll have to fix venues.longitude as well.

这篇关于Rails-Postgres上的gmaps4rails gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆