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的陣列內的整數變量的對象?
謝謝!
而不是'Notification.where( 「?UIP @>」, '{} IP')''嘗試Notification.where( 「?UIP @>」 ,'{'+ ip +'}')' – 2015-02-10 12:36:54