这在我的开发环境中有效,但是当我尝试通过 Heroku 和 Postgres 运行时,出现此错误。
2012-04-07T21:35:14+00:00 app[web.1]: ActiveRecord::StatementInvalid
(PG::Error: ERROR: operator does not exist: integer == integer
2012-04-07T21:35:14+00:00 app[web.1]: LINE 1: ...."value") AS avg_id FROM "datapoints"
WHERE (habit_id == 1)
2012-04-07T21:35:14+00:00 app[web.1]: HINT: No operator matches the given name and argument type(s).
You might need to add explicit type casts.
2012-04-07T21:35:14+00:00 app[web.1]: : SELECT AVG("datapoints"."value") AS avg_id FROM "datapoints
WHERE (habit_id == 1)):
下面是我的 Controller 中的代码行:
Datapoint.average(:value, :conditions => ['habit_id == ?', self.habit_id])
我对 Rails 还很陌生,所以这很可能是一个非常简单的错误 - 对我在这里做错了什么有什么想法吗?
最佳答案
只需使用一个等号。
Postgresql(和 SQL 标准)使用单个等号 (=) 进行比较:http://www.postgresql.org/docs/8.0/static/functions-comparison.html
与编程语言不同,SQL 不需要支持使用它进行赋值,除非在更新子句中。因此,它不需要大多数编程语言所需的双等于运算符 (==) 进行比较。
关于ruby-on-rails-3 - Rails 3.2 Heroku PG 错误 - 'operator does not exist: integer == integer',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10059034/