Saurabh Saurabh - 5 months ago 22
Ruby Question

Rails 4.1. Heroku ActiveRecord::StatementInvalid (PG::UndefinedFunction: ERROR: operator does not exist: numeric - character varying

Here is my controller where I hit the query:

def search_by_location
@stores = Store.includes(:images).near([params[:latitude], params[:longitude]], 10)
end


All works great on local machine. On PG, Heroku I get an error.

Here is the logs :

2016-06-23T08:04:56.392549+00:00 heroku[router]: at=info method=POST path="/api/customer/search_by_location?type=daeal&page=1" host=loyelte.herokuapp.com request_id=0364888a-3866-4acd-bea0-17a4bf4eaeee fwd="122.162.251.21" dyno=web.1 connect=0ms service=14ms status=500 bytes=1669
2016-06-23T08:04:56.362768+00:00 app[web.1]: Started POST "/api/customer/search_by_location?type=daeal&page=1" for 122.162.251.21 at 2016-06-23 08:04:56 +0000
2016-06-23T08:04:56.365114+00:00 app[web.1]: Processing by Customer::CustomersController#search_by_location as */*
2016-06-23T08:04:56.365163+00:00 app[web.1]: Parameters: {"latitude"=>"28", "longitude"=>"77", "type"=>"daeal", "page"=>"1", "customer"=>{}}
2016-06-23T08:04:56.366751+00:00 app[web.1]: Authentication Load (0.8ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."auth_token" = $1 AND "authentications"."subject_type" = $2 LIMIT 1 [["auth_token", "41360d38bef547ab8e9ae2ce47459c53"], ["subject_type", "Customer"]]
2016-06-23T08:04:56.368775+00:00 app[web.1]: Customer Load (0.9ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = $1 LIMIT 1 [["id", 3]]
2016-06-23T08:04:56.371919+00:00 app[web.1]: Store Load (1.4ms) SELECT stores.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((28.0 - stores.latitude) * PI() / 180 / 2), 2) + COS(28.0 * PI() / 180) * COS(stores.latitude * PI() / 180) * POWER(SIN((77.0 - stores.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((stores.longitude - 77.0) / 57.2957795), ((stores.latitude - 28.0) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "stores" WHERE (stores.latitude BETWEEN 27.85526821688915 AND 28.14473178311085 AND stores.longitude BETWEEN 76.83608111706583 AND 77.16391888293417 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((28.0 - stores.latitude) * PI() / 180 / 2), 2) + COS(28.0 * PI() / 180) * COS(stores.latitude * PI() / 180) * POWER(SIN((77.0 - stores.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 10) ORDER BY distance ASC
2016-06-23T08:04:56.372212+00:00 app[web.1]: Completed 500 Internal Server Error in 7ms (ActiveRecord: 3.1ms)
2016-06-23T08:04:56.373468+00:00 app[web.1]:
2016-06-23T08:04:56.373476+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::UndefinedFunction: ERROR: operator does not exist: numeric - character varying
2016-06-23T08:04:56.373477+00:00 app[web.1]: LINE 1: ...*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((28.0 - stores.l...
2016-06-23T08:04:56.373477+00:00 app[web.1]: ^
2016-06-23T08:04:56.373478+00:00 app[web.1]: HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
2016-06-23T08:04:56.373479+00:00 app[web.1]: : SELECT stores.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((28.0 - stores.latitude) * PI() / 180 / 2), 2) + COS(28.0 * PI() / 180) * COS(stores.latitude * PI() / 180) * POWER(SIN((77.0 - stores.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((stores.longitude - 77.0) / 57.2957795), ((stores.latitude - 28.0) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "stores" WHERE (stores.latitude BETWEEN 27.85526821688915 AND 28.14473178311085 AND stores.longitude BETWEEN 76.83608111706583 AND 77.16391888293417 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((28.0 - stores.latitude) * PI() / 180 / 2), 2) + COS(28.0 * PI() / 180) * COS(stores.latitude * PI() / 180) * POWER(SIN((77.0 - stores.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 10) ORDER BY distance ASC):

Answer

As per our comments with OP, he is using geocoder gem. The table fields for latitude and longitude fields must be set to float.