簡體   English   中英

ActiveRecord :: StatementInvalid:PG錯誤:錯誤:運算符不存在:沒有時區的時間戳> =整數

[英]ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist: timestamp without time zone >= integer

我在比較Rails 2項目中的日期時遇到麻煩。 在我的一個查詢中,條件之一是:

:conditions => ["bills.created_at >= #{beginning.to_s.delete("-")} AND bills.created_at <= #{ending.to_s.delete("-")}"]

我分別通過"2013-10-16""2013-12-15"作為beginningending 這在我的暫存環境中有效,但在我的生產環境中無效:

    ActiveRecord::StatementInvalid: PGError: ERROR:  operator does not exist: timestamp without time zone >= integer
    LINE 1: SELECT * FROM "bills" WHERE (created_at >= 20131016)

我該如何解決? 我看到了這個heroku Postgres錯誤-如果沒有時區=整數,運算符不存在時間戳,但是它沒有幫助。

您的查詢絕對不同於ActiveRecord查詢語法。 假設您至少使用Rails 3,並且beginningending都是Time對象:

Bill.where("created_at >= ? AND created_at <= ?", beginning, ending)

或者您也可以使用BETWEEN傳遞Ruby Range

Bill.where(created_at: beginning..ending)

請避免像剛才那樣在查詢中插值。 結果查詢無法防止SQL注入。

您可能還需要查看ActiveRecord文檔,以了解一些如何正確使用它的信息。

對於Rails <3,您應該將條件傳遞給find方法。

Bill.find(:all, :conditions => "created_at >= ? AND created_at <= ?", beginning, ending)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM