2012-08-16 3 views
0

현재이 하나의 ActiveRecord 쿼리에서 오류가 계속 발생하는 Heroku 앱이 있습니다.heroku activerecord 쿼리 오류

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

'CATEGORY_ID과의 첫 번째 쿼리가 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 

사람이이 문제를 해결하는 방법을 알고 있나요을? 감사.

답변

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

do where(category_id: category) 대신 데이터베이스에 영향을받지 않습니다.

개발 환경에 mysql 또는 sqlite가 있습니까?

+0

그럼 null 문과 마찬가지로 'where (category : nil)' – Iamvery

+0

정말 도움이되었습니다. – pandapirate