0

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.)

7
  • Can you give us a concrete example (eg what the data would look like in the table and what you'd pass to it and what the result would like like - if you were god and could make it however you like? :) Commented Sep 24, 2014 at 7:03
  • updated, is it any clearer now? Commented Sep 24, 2014 at 7:15
  • So you'd like to "return a uniquified array of software_for_products from the rows that have one of the given OSs"? Commented Sep 24, 2014 at 7:21
  • Actually I'd like for it to return a relation for each match for further processing. Commented Sep 24, 2014 at 7:24
  • 1
    sorry, I mucked my example up completely in the first edit. I fixed it now to better correspond what I'm looking for. Commented Sep 24, 2014 at 7:43

1 Answer 1

1

Please look into this gem.

And then do a query like

selected_products = ['Apple', 'Linux']
Software.where.overlap(osses: selected_products)
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.