2015-02-10 60 views
1
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的阵列内的整数变量的对象?

谢谢!

+0

而不是'Notification.where( “?UIP @>”, '{} IP')''尝试Notification.where( “?UIP @>” ,'{'+ ip +'}')' – 2015-02-10 12:36:54

回答

4

有多种方式,你可以在一个Postgres阵列查询使用ActiveRecord:

固定查询:

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

使用 '任意':

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

第一个作品谢谢:),第二个似乎不是:'PG :: UndefinedFunction:错误:运算符不存在:bigint [] && integer []' – Laser 2015-02-10 21:04:49

+0

我建议你跟@ @一起去,因为你可以用杜松子酒将它索引。 – sandeep 2015-02-11 10:48:57

+0

第一个似乎没有使用字符串数组,但第二个是 – Kazik 2015-07-27 05:23:36

0

使用它。我认为这是有帮助的,你

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

OR

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

'PG :: InvalidTextRepresentation:ERROR:malformed array literal:“1”' – Laser 2015-02-10 21:08:00

+0

嗨激光,我现在改变了我的答案..你用这种方法..我认为它将对您有所帮助 – Angu 2015-02-11 09:23:49

1

试试这一个。我刚搬到你的括号进入条件:

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

'PG :: SyntaxError:ERROR:语法错误处于或接近“{”' – Laser 2015-02-10 21:07:37

2

您需要从输入中创建一个数组:

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) 

如果您只想测试一个元素,那么也可以使用重叠(&&)运算符(它应该更快一些)。或者,你可以使用数组ANY结构:

Notification.where("? = ANY (uip)", ip) 
+0

第三和第四个工作谢谢:),前两个渲染错误:'PG :: UndefinedFunction:错误:运算符不存在:bigint [] @>整数[]'和'TypeError:分别将Fixnum隐式转换为String' – Laser 2015-02-10 21:06:51

+0

ANY会比@>更快吗? – Laser 2015-02-10 21:10:49

+0

@激光然后你需要无处不在的演员阵容; “ANY”只是简单一点,用较小的套件可以工作,但不可转位。 '@>'和'&&'运算符可以用['gin']索引(http://www.postgresql.org/docs/current/static/indexes-types.html)。 – pozs 2015-02-11 08:50:01