2015-02-10 33 views
1
Notification.where("uip @> ?", '{1}') 

工作正常,並返回其所有UIP陣列包含1如何查詢postgres數組在Ruby on Rails整數使用變量?

如果我嘗試但是具有可變以下,我沒有這樣的運氣通知:

ip = 1 

Notification.where("uip @> ?", '{ip}') 

返回錯誤:

Notification Load (1.8ms) SELECT "notifications".* FROM "notifications" WHERE (uip @> '{ip}') 
PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "ip" 
LINE 1: ...otifications".* FROM "notifications" WHERE (uip @> '{ip}') 
                   ^
: SELECT "notifications".* FROM "notifications" WHERE (uip @> '{ip}') 
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "ip" 
LINE 1: ...otifications".* FROM "notifications" WHERE (uip @> '{ip}') 
                   ^
: SELECT "notifications".* FROM "notifications" WHERE (uip @> '{ip}') 

而另一嘗試用:

Notification.where("uip @> ?", ip) 

給出了錯誤:

SELECT "notifications".* FROM "notifications" WHERE (uip @> 1) 
PG::UndefinedFunction: ERROR: operator does not exist: bigint[] @> integer 
LINE 1: ...CT "notifications".* FROM "notifications" WHERE (uip @> 1) 
                   ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
: SELECT "notifications".* FROM "notifications" WHERE (uip @> 1) 
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: bigint[] @> integer 
LINE 1: ...CT "notifications".* FROM "notifications" WHERE (uip @> 1) 
                   ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
: SELECT "notifications".* FROM "notifications" WHERE (uip @> 1) 

所以,我怎麼能簡單地找到在軌Postgres的陣列內的整數變量的對象?

謝謝!

+0

而不是'Notification.where( 「?UIP @>」, '{} IP')''嘗試Notification.where( 「?UIP @>」 ,'{'+ ip +'}')' – 2015-02-10 12:36:54

回答

4

有多種方式,你可以在一個Postgres陣列查詢使用ActiveRecord:

固定查詢:

ip = 1  
Notification.where("uip @> '{?}'", ip) 

使用 '任意':

ip = 1 
Notification.where("uip && ARRAY[?]", ip) 
+0

第一個作品謝謝:),第二個似乎不是:'PG :: UndefinedFunction:錯誤:運算符不存在:bigint [] && integer []' – Laser 2015-02-10 21:04:49

+0

我建議你跟@ @一起去,因爲你可以用杜松子酒將它索引。 – sandeep 2015-02-11 10:48:57

+0

第一個似乎沒有使用字符串數組,但第二個是 – Kazik 2015-07-27 05:23:36

0

使用它。我認爲這是有幫助的,你

ip = 1 
Notification.where("uip @> ?", "#{ip.to_s.to_i}") 

OR

Notification.where("uip @>?",ip.to_s.to_i) 
+0

'PG :: InvalidTextRepresentation:ERROR:malformed array literal:「1」' – Laser 2015-02-10 21:08:00

+0

嗨激光,我現在改變了我的答案..你用這種方法..我認爲它將對您有所幫助 – Angu 2015-02-11 09:23:49

1

試試這一個。我剛搬到你的括號進入條件:

ip = 1 
Notification.where("uip @> '{?}'", ip) 
+0

'PG :: SyntaxError:ERROR:語法錯誤處於或接近「{」' – Laser 2015-02-10 21:07:37

2

您需要從輸入中創建一個數組:

Notification.where("uip @> CAST(ARRAY[?] AS BIGINT[])", ip) 
// or 
Notification.where("uip @> CAST(? AS BIGINT[])", '{' + ip.to_s + '}') 
// or 
Notification.where("uip @> CAST('{' || ? || '}' AS BIGINT[])", ip) 

如果您只想測試一個元素,那麼也可以使用重疊(&&)運算符(它應該更快一些)。或者,你可以使用數組ANY結構:

Notification.where("? = ANY (uip)", ip) 
+0

第三和第四個工作謝謝:),前兩個渲染錯誤:'PG :: UndefinedFunction:錯誤:運算符不存在:bigint [] @>整數[]'和'TypeError:分別將Fixnum隱式轉換爲String' – Laser 2015-02-10 21:06:51

+0

ANY會比@>更快嗎? – Laser 2015-02-10 21:10:49

+0

@激光然後你需要無處不在的演員陣容; 「ANY」只是簡單一點,用較小的套件可以工作,但不可轉位。 '@>'和'&&'運算符可以用['gin']索引(http://www.postgresql.org/docs/current/static/indexes-types.html)。 – pozs 2015-02-11 08:50:01