user3797265
user3797265

Reputation: 13

Query column of arrays with array

I have a database column with the datatype array. How can I query an array with an array in rails? For example, I have an array of software for an OS in my Product table and I want to query them like this:

selected_products = ['Apple', 'Linux']

Software.where("'#{selected_products}' = ANY (osses)")

edit:

example of Software table:

id  |  software    |        osses         |
_______________________________________________________

1     product 1     {'Windows', 'Apple', 'Android'}
2     product 2     {'Android', 'Linux'}
3     product 3     {'Windows,', 'Android'}
4     product 4     {'Windows'}

In which case I'd like the query to return id's 1 and 2 (since id 1 has a match in Apple and id 2 has a match in Linux.)

Upvotes: 0

Views: 103

Answers (1)

Sarathkumar
Sarathkumar

Reputation: 71

Please look into this gem.

And then do a query like

selected_products = ['Apple', 'Linux']
Software.where.overlap(osses: selected_products)

Upvotes: 1

Related Questions