Ruby on rails 如何使用变量在RubyonRails中查询postgres数组中的整数?

Ruby on rails 如何使用变量在RubyonRails中查询postgres数组中的整数?,ruby-on-rails,ruby,arrays,postgresql,ruby-on-rails-4,Ruby On Rails,Ruby,Arrays,Postgresql,Ruby On Rails 4,工作正常,并返回其uip数组包含1的所有通知 但是,如果我使用变量尝试以下操作,我就没有这样的运气: Notification.where("uip @> ?", '{1}') 返回错误: ip = 1 Notification.where("uip @> ?", '{ip}') Notification.where("uip @> ?", ip) 另一次尝试是: Notification Load (1.8ms) SELECT "notifications".

工作正常,并返回其uip数组包含1的所有通知

但是,如果我使用变量尝试以下操作,我就没有这样的运气:

Notification.where("uip  @> ?", '{1}')
返回错误:

ip = 1

Notification.where("uip  @> ?", '{ip}')
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}')
给出了错误:

ip = 1

Notification.where("uip  @> ?", '{ip}')
Notification.where("uip  @> ?", ip)
那么,我如何通过rails中postgres数组中的整数变量简单地查找对象呢


谢谢

使用它。我认为这对你有帮助

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)


使用它。我认为这对你有帮助

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)


试试这个。我刚把你的牙套换好:

Notification.where("uip @>?",ip.to_s.to_i)

试试这个。我刚把你的牙套换好:

Notification.where("uip @>?",ip.to_s.to_i)

使用ActiveRecord可以通过多种方式在postgres数组中进行查询:

修复您的查询:

ip = 1
Notification.where("uip  @> '{?}'", ip)
使用“ANY”:

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

使用ActiveRecord可以通过多种方式在postgres数组中进行查询:

修复您的查询:

ip = 1
Notification.where("uip  @> '{?}'", ip)
使用“ANY”:

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

您需要根据输入创建一个数组:

ip = 1
Notification.where("uip && ARRAY[?]", ip)
如果只想测试一个元素,也可以使用重叠(
&&&
)操作符(应该更快一点)。或者,您可以对数组使用
ANY
构造:

Notification.where("uip @> CAST(ARRAY[?] AS BIGINT[])", ip)
// or
Notification.where("uip @> CAST(? AS BIGINT[])", '{' + ip.to_s + '}')
// or
Notification.where("uip @> CAST('{' || ? || '}' AS BIGINT[])", ip)

您需要根据输入创建一个数组:

ip = 1
Notification.where("uip && ARRAY[?]", ip)
如果只想测试一个元素,也可以使用重叠(
&&&
)操作符(应该更快一点)。或者,您可以对数组使用
ANY
构造:

Notification.where("uip @> CAST(ARRAY[?] AS BIGINT[])", ip)
// or
Notification.where("uip @> CAST(? AS BIGINT[])", '{' + ip.to_s + '}')
// or
Notification.where("uip @> CAST('{' || ? || '}' AS BIGINT[])", ip)

代替
通知。where(“uip@>?”,{ip}')
试试
通知。where(“uip@>?”,{+ip+}')
代替
通知。where(“uip@>?”,{ip}')
试试
通知。where(“uip@>?”,{+ip+}')
第一款有效谢谢:),第二个似乎不存在:
PG::UndefinedFunction:ERROR:运算符不存在:bigint[]&&integer[]
我建议您使用@>,因为您可以使用gin对其进行索引。第一个似乎不适用于字符串数组,但第二个不适用于第一个,谢谢:),第二个似乎不存在:
PG::UndefinedFunction:错误:运算符不存在:bigint[]&&integer[]
我建议您使用@>,因为您可以使用gin对其进行索引。第一个似乎不适用于字符串数组,但第二个适用于第三个和第四个,谢谢:),前两个渲染错误:
PG::UndefinedFunction:ERROR:运算符不存在:bigint[]@>integer[]
TypeError:没有将Fixnum分别隐式转换为字符串
ANY是否比@>快?@Laser则需要到处执行exeplicit强制转换<代码>任何都很简单&适用于较小的集合,但不可索引。
@>
&&
运算符可以用.3和第4个工作索引,谢谢:),前两个渲染错误:
PG::UndefinedFunction:错误:运算符不存在:bigint[]@>integer[]
类型错误:没有将Fixnum分别隐式转换为字符串
ANY会比@>快吗?@激光则到处都需要exeplicit强制转换<代码>任何都很简单&适用于较小的集合,但不可索引。
@>
&&
运算符可以用索引。
PG::SyntaxError:ERROR:syntaxer错误位于或靠近“{”
PG::InvalidTextRepresentation:错误:格式错误的数组文字:“1”
Hi laser,我现在更改了答案..您使用此方法..我认为这将对您有所帮助
PG::InvalidTextRepresentation:错误:格式错误的数组文字:“1”
Hi laser,我现在更改了答案..您使用此方法..我认为这将对您有所帮助