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