Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can use the usual array indexing notation but remember that SQL arrays are 1-based rather than 0-based:

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

The fine manual has more on accessing arrays.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.