0

In my rails psql application I have an emails array column. The element in the first index of this column is the primary email. I want to be able to search the db for all people with primary email x.

2 Answers 2

1

No need to test for array inclusion. Just get the first element and compare that to the string:

User.where("emails[1] = ?", "[email protected]")
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, easier to understand. I did some Benchmark.measure to compare with the Array inclusion answer and there seems to be little difference in Performance.
0

I could not see this in the documentation but thanks to this answer I can see that in Postgres 9.5 or older you can use

arr[1:1]

syntax to look only in the first element

So this is the answer

User.where("emails[1:1] @> ARRAY[?]", "[email protected]")

Comments

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.