0

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?

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.