Steve
Steve

Reputation: 474

How can I query the db for all items in a table where a value meets one of the values in an array in Rails?

This is probably better explained with an example. I have a documents table that has a country attribute, for example Document.first.country could return 'DE'. I have an array of country codes, for called eu_countries with the value ["AT", "BE", "BG", "CY", "CZ", "DE"...] and I would like to query the db and return only documents that have a country code that is in the array.

Something with the same functionality as: Documents.where(country == "AT" or "BE" or "BG" or "CY" or "CZ" or "DE"...)

Upvotes: 0

Views: 138

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51191

It's quite simple

Document.where(country: eu_countries)

It is transferred to SQL similar to this:

select documents.* from documents where documents.country IN (values) 

Upvotes: 1

Related Questions