arpansac
arpansac

Reputation: 38

Rails: Find all records where string column contains all words from an array of strings

How to filter all the records from a table where a column 'name' is like (in any order) all the values from an array of strings

Upvotes: 1

Views: 1646

Answers (2)

Carlos J García
Carlos J García

Reputation: 494

If you're using postgres DB please check the following answer since postgress has arrays.

You might want to do it at application level if your db is small.

If REGEXP is viable to you then the @khiav reoy answer is the best you could do.

Upvotes: 2

khiav reoy
khiav reoy

Reputation: 1403

You could use REGEXP

Model.where('name REGEXP ?', array_of_string.join('|'))

Upvotes: 3

Related Questions