简体   繁体   English

是否可以使用Rails 4查询postgres数组的特定索引?

[英]Is it possible to query a specific index of a postgres array with Rails 4?

With a database column migrated like this: 使用如下迁移的数据库列:

add_column :documents, :array_column, :string, array: true

I understand it's possible to do this to query any element of the array: 我知道可以这样做来查询数组的任何元素:

Document.where("'foo' = ANY (array_column)")

My question is whether or not I can specifically query the second (or any other single) element of an array? 我的问题是我是否可以专门查询数组的第二个(或任何其他单个)元素?

You can use the usual array indexing notation but remember that SQL arrays are 1-based rather than 0-based: 您可以使用常用的数组索引表示法,但请记住,SQL数组是基于1的而不是基于0的:

Document.where("array_column[2] = 'foo'")

The fine manual has more on accessing arrays . 精细的手册有更多关于访问数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM