Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm writing a Rails 4 app and have a string array column on one of my models called identifiers. When the user passes in a list of identifiers, what's the syntax to fetch the rows where any of the given identifiers match any of the stored identifiers?

I'm trying where('ARRAY[?] == any(identifiers)', ids), but that doesn't seem to work.

share|improve this question
 
Is this column being serialized in your model? –  BroiSatse yesterday
 
No serialization happens... Postgres natively supports arrays on columns. –  Sudhir Jonathan 12 hours ago
add comment

1 Answer

Use the array operator "overlap": &&

SELECT * FROM tbl WHERE ARRAY[1,2,3] && identifiers

Or:

SELECT * FROM tbl WHERE '{1,2,3}'::int[] && identifiers

You did not disclose the exact type, which must match, of course. My example is for integer arrays.

This form can utilize a GIN index on the identifiers column:

CREATE INDEX tbl_identifiers_gin_idx ON tbl USING GIN (identifiers);
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.