0

postgresqlで動作しないmysqlクエリがあります:-

@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
4

1 に答える 1

0

はいバックティックはMySql-ismでした

''を""で削除した後、正常に機能しました。

変更後のクエリは次のとおりです:-

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")
于 2012-09-21T10:47:18.683 に答える