?", '{1}') 工作正常并返回其 uip 数组包含 1 的所有通知。 但是,如果我尝试使用变量执行以下操作,我就没有这样的运气了: ip -6ren">
gpt4 book ai didi

ruby-on-rails - 您如何使用变量在 ruby​​ on rails 中查询 postgres 数组的整数?

转载 作者:太空宇宙 更新时间:2023-11-03 17:15:19 24 4
gpt4 key购买 nike

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 数组中的整数变量找到对象呢?

谢谢!

最佳答案

有多种方法可以使用 ActiveRecord 在 postgres 数组中查询:

修复您的查询:

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

使用“任何”:

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

关于ruby-on-rails - 您如何使用变量在 ruby​​ on rails 中查询 postgres 数组的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28431755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com