2

这在我的开发环境中有效,但是当我尝试通过 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)):

以下是我的控制器中的代码行:

Datapoint.average(:value, :conditions => ['habit_id == ?', self.habit_id])

我对 Rails 很陌生,所以这很容易成为一个非常简单的错误——对我在这里做错了什么有任何想法吗?

4

1 回答 1

5

只需使用一个等号。

Postgresql(和 SQL 标准)使用单个等号 (=) 进行比较: http ://www.postgresql.org/docs/8.0/static/functions-comparison.html

与编程语言不同,SQL 不需要支持将其用于赋值,除非在更新子句中。因此,它不需要大多数编程语言所需的双等号运算符 (==) 进行比较。

于 2012-04-07T23:02:06.423 回答