【发布时间】:2015-02-10 12:30:56
【问题描述】:
Notification.where("uip @> ?", '{1}')
工作正常并返回所有 uip 数组包含 1 的通知。
但是,如果我使用变量尝试以下操作,我就没有这样的运气:
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)
那么我怎样才能通过 Rails 中的 postgres 数组中的整数变量简单地找到对象呢?
谢谢!
【问题讨论】:
-
代替
Notification.where("uip @> ?", '{ip}')试试Notification.where("uip @> ?", '{' + ip + '}')
标签: ruby-on-rails ruby arrays postgresql ruby-on-rails-4