pandapirate
pandapirate

Reputation: 19

heroku activerecord query errors

I currently have an app on Heroku that keeps throwing errors from this one ActiveRecord query.

if category == "-1"
    where('category_id IS null')
else
    where('category_id IS ?', category)
end

The first query with the 'category_id IS null works just fine but the second query throws such an error:

2012-08-16T18:58:03+00:00 app[web.1]:
2012-08-16T18:58:03+00:00 app[web.1]:
2012-08-16T18:58:03+00:00 app[web.1]: Started GET "/items?cpath=4" for 204.154.121.30            at 2012-08-16 18:58:03 +0000
2012-08-16T18:58:08+00:00 app[web.1]:
2012-08-16T18:58:08+00:00 app[web.1]: ActiveRecord::StatementInvalid (PGError: ERROR:  syntax error at or near "4"
2012-08-16T18:58:08+00:00 app[web.1]: LINE 1: SELECT COUNT(*) FROM "items"  WHERE (category_id IS 4)
2012-08-16T18:58:08+00:00 app[web.1]:                                                             ^
2012-08-16T18:58:08+00:00 app[web.1]:   app/controllers/items_controller.rb:30:in `index'
2012-08-16T18:58:08+00:00 app[web.1]: : SELECT COUNT(*) FROM "items"  WHERE (category_id IS 4)):
2012-08-16T18:58:08+00:00 app[web.1]:
2012-08-16T18:58:08+00:00 app[web.1]:
2012-08-16T18:58:08+00:00 app[web.1]: cache: [GET /items?cpath=4] miss
2012-08-16T18:58:08+00:00 app[web.1]:   Processing by ItemsController#index as HTML
2012-08-16T18:58:08+00:00 app[web.1]:   Parameters: {"cpath"=>"4"}
2012-08-16T18:58:08+00:00 app[web.1]: Completed 500 Internal Server Error in 3ms
2012-08-16T18:58:08+00:00 heroku[router]: GET yisifahan.herokuapp.com/items?cpath=4 dyno=web.1 queue=0 wait=0ms service=
5026ms status=500 bytes=728
2012-08-16T18:58:08+00:00 heroku[web.1]: Error R12 (Exit timeout) -> At least one process failed to exit within 10 seconds of SIGTERM
2012-08-16T18:58:08+00:00 heroku[web.1]: Stopping remaining processes with SIGKILL
2012-08-16T18:58:09+00:00 heroku[web.1]: Process exited with status 137

Does anyone know how to fix this problem? Thanks.

Upvotes: 0

Views: 133

Answers (2)

Iamvery
Iamvery

Reputation: 284

I believe the IS comparison operator is invalid for equality. (http://www.postgresql.org/docs/9.2/static/functions-comparison.html)

So you're snippet should look something like:

if category == "-1"
  where('category_id IS null')
else
  where('category_id = ?', category)
end

Honestly you should have some tests checking your code for these cases. It shouldn't have made its way onto the server as such. I would also suggest you use a postgres database in your development environment so you can make sure you're code is valid against it's architecture.

Upvotes: 2

Ismael
Ismael

Reputation: 16730

do where(category_id: category) instead to be database agnostic.

You have mysql or sqlite in development env right?

Upvotes: 2

Related Questions