简体   繁体   English

heroku activerecord查询错误

[英]heroku activerecord query errors

I currently have an app on Heroku that keeps throwing errors from this one ActiveRecord query. 我目前在Heroku上有一个应用程序,该应用程序不断从此ActiveRecord查询中引发错误。

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: 第一个查询的'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

Does anyone know how to fix this problem? 有谁知道如何解决这个问题? Thanks. 谢谢。

do where(category_id: category) instead to be database agnostic. where(category_id: category)而不是数据库不可知的。

You have mysql or sqlite in development env right? 您在开发环境中拥有mysql或sqlite,对吗?

I believe the IS comparison operator is invalid for equality. 我相信IS比较运算符对相等性无效。 (http://www.postgresql.org/docs/9.2/static/functions-comparison.html) (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. 我还建议您在开发环境中使用postgres数据库,这样可以确保您的代码对它的体系结构有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM