【问题标题】:Active Record Placeholder/Subset conditionsActive Record 占位符/子集条件
【发布时间】:2012-08-09 18:11:48
【问题描述】:

我有以下疑问:

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 )

有什么帮助吗?

谢谢

【问题讨论】:

  • 这不是有效的语法。你期望它做什么?也许您想创建一个值数组并查看数组中是否有另一个值?

标签: sql ruby-on-rails postgresql activerecord


【解决方案1】:

最简单的做法是将两个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 的 => 需要两边都包含字符串)。

【讨论】:

    猜你喜欢
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2011-01-28
    • 1970-01-01
    相关资源
    最近更新 更多