Mysql2查询无法在Heroku上运行

时间:2012-09-21 06:41:10

标签: mysql ruby-on-rails ruby-on-rails-3 postgresql activerecord

我有一个mysql查询,它不适用于postgresql: -

@events = Event.all_with_distance([current_user.geo_lat, current_user.geo_lng]).where("start > ?", Time.zone.now).order("distance").order("start DESC").where("title like ?", "%#{params[:q]}%")

def self.all_with_distance(origin)
    distance_sql = sql_for_distance(origin)
    select("#{table_name}.*, #{distance_sql} AS distance").select("`locations`.`geo_lat`, `locations`.`geo_lng`, `locations`.`name` as location_name").joins(:location).where("#{distance_sql} < 50")
  end

这是我得到的错误: -

PG::Error: ERROR:  syntax error at or near "."
LINE 1: ...SIN(RADIANS(26.8465108)) * SIN(RADIANS(`locations`.`geo_lat`...

生成的查询: -

SELECT events.*, (ACOS( SIN(RADIANS(26.8465108)) * SIN(RADIANS(`locations`.`geo_lat`)) + COS(RADIANS(26.8465108)) * COS(RADIANS(`locations`.`geo_lat`)) * COS(RADIANS(`locations`.`geo_lng`) - RADIANS(80.9466832)) ) * 6378.135)  AS distance, `locations`.`geo_lat`, `locations`.`geo_lng`, `locations`.`name` as location_name FROM "events" INNER JOIN "locations" ON "locations"."id" = "events"."location_id" WHERE ((ACOS( SIN(RADIANS(26.8465108)) * SIN(RADIANS(`locations`.`geo_lat`)) + COS(RADIANS(26.8465108)) * COS(RADIANS(`locations`.`geo_lat`)) * COS(RADIANS(`locations`.`geo_lng`) - RADIANS(80.9466832)) ) * 6378.135)  < 50) AND (start > '2012-09-21 06:40:18.964016') AND (title like '%%') ORDER BY distance, start DESC

1 个答案:

答案 0 :(得分:0)

是返回Ticks是MySql-ism

删除''with“后,它工作正常。

以下是更改后的查询: -

select("#{table_name}.*, #{distance_sql} AS distance").select('"locations"."geo_lat", "locations"."geo_lng", "locations"."name" as location_name').joins(:location).where("#{distance_sql} < 50")