簡體   English   中英

活動記錄占位符/子集條件

[英]Active Record Placeholder/Subset conditions

我有以下查詢:

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 )

有幫助嗎?

謝謝

最容易做的事情是鏈上的兩個where調用起來,讓ActiveRecord的找出所需要的SQL:

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

這將在SQL中為您生成IN ,因此數據庫應該看到如下內容:

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

錯誤消息告訴您PostgreSQL中沒有=>運算符,兩邊都有整數。 那是因為=>是Ruby語法,而不是PostgreSQL語法(當然,除非你安裝了hstore,而hstore's =>兩邊的字符串)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM