Notification.where("uip @> ?", '{1}')
Works fine and returns all the notifications whose uip array contain a 1.
If I try the following with a variable, however, I have no such luck:
ip = 1
Notification.where("uip @> ?", '{ip}')
Return the error:
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}')
And another attempt with:
Notification.where("uip @> ?", ip)
Gives the error:
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)
So how can I simply find objects by an integer variable inside a postgres array in rails?
Thanks!
Notification.where("uip @> ?", '{ip}')tryNotification.where("uip @> ?", '{' + ip + '}')