2012-08-16 96 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