![](/img/trans.png)
[英]Postgresql-specific database error: “ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist”
[英]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"
作为beginning
和ending
。 这在我的暂存环境中有效,但在我的生产环境中无效:
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,并且beginning
和ending
都是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.