0

我有以下查询:

Score.where("build_id => ? AND metric_id => ? ",params[:buildIds], params[:metricIds])

其中 params[:buildIds], params[:metricIds] 是整数数组。我收到此错误:

PG::Error: ERROR:  operator does not exist: integer => integer
LINE 1: SELECT "scores".* FROM "scores"  WHERE (build_id => 1,2 AND ...
                                                         ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "scores".* FROM "scores"  WHERE (build_id => 1,2 AND metric_id => 1,13 )

有什么帮助吗?

谢谢

4

1 回答 1

2

最简单的做法是将两个where调用链接在一起,让 ActiveRecord 找出需要什么 SQL:

Score.where(:build_id => params[:buildIds]).where(:metric_id => params[:metricIds])

这将为您在 SQL 中生成INs,因此数据库应该看到如下内容:

where build_id in (1, 2) and metric_id in (1, 13)

错误消息告诉您 PostgreSQL 中没有=>两边都有整数的运算符。那是因为=>是 Ruby 语法,而不是 PostgreSQL 语法(当然,除非您安装了 hstore 并且 hstore 的=>两边都需要字符串)。

于 2012-08-09T18:42:44.913 回答