0

我目前在 Heroku 上有一个应用程序,它不断地从这个 ActiveRecord 查询中抛出错误。

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

'category_id IS null 的第一个查询可以正常工作,但第二个查询会引发这样的错误:

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

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

4

2 回答 2

2

where(category_id: category)改为与数据库无关。

您在开发环境中有 mysql 或 sqlite 吗?

于 2012-08-16T19:15:58.310 回答
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 数据库,以便确保您的代码对其架构有效。

于 2012-08-16T19:16:32.343 回答