0

I have an Objective model which has an attribute called as labels whose values are array data type. I need to query all the Objectives whose labels attribute has values that are present in some particular array.

For Example: I have an array

a = ["textile", "blazer"]

the Objective.labels may have values as ["textile, "ramen"]

I need to return all objectives that might have either "textile" or "blazer" as one of their labels array values

I tried the following:

Objective.where("labels @> ARRAY[?]::varchar[]", ["textile"])

This returns some records.Now when I try

Objective.where("labels @> ARRAY[?]::varchar[]", ["textile", "Blazer"])

I expect it to return all Objectives which contains at-least one of the labels array value as textile or blazer.

However, it returns an empty array. Any Solutions?

1 Answer 1

1

Try && overlap operator.

overlap (have elements in common)

Objective.where("labels && ARRAY[?]::varchar[]", ["textile", "Blazer"])

If you have many rows, a GIN index can speed it up.

Sign up to request clarification or add additional context in comments.

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.