2012-08-16 53 views
0

我目前在Heroku上有一个应用程序,它一直在抛出这个ActiveRecord查询中的错误。heroku activerecord查询错误

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

与“CATEGORY_ID第一个查询为空工作得很好,但第二个查询抛出这样的错误:

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 

有谁知道如何解决这个问题?谢谢。

回答

2

我认为IS比较运算符对于平等是无效的。 (http://www.postgresql.org/docs/9.2/static/functions-comparison.html)

所以你片断应该是这个样子:

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

老实说你应该有一些测试检查你的代码是否有这些情况。它不应该如此进入服务器。我还建议你在你的开发环境中使用postgres数据库,这样你就可以确保你的代码对它的体系结构有效。

2

where(category_id: category)而不是数据库不可知的。

你有mysql或sqlite开发环境?

+0

所以,对于null语句以及'where where(category:nil)' – Iamvery 2012-08-16 19:17:35

+0

谢谢,这非常有用 – pandapirate 2012-08-16 21:02:08