Multiple like conditions in active record

activerecord, ruby-on-rails

Solution

Try this

names = ['alice', 'bob', 'mark']
Person.where('name REGEXP ?',names.join('|'))

While matching like with multiple values on single column you can use REGEXP instead of `LIKE` this will generate a sql statement with in clause

to check the sql generated just add `.to_sql`

Person.where('name REGEXP ?',names.join('|')).to_sql

Problem

I need to check my records against multiple like statements, what is the best way to do so? i.e. ``` names = ['alice', 'bob', 'mark'] ``` I've got table lets say People in my database or (Peoples) so I want to select all people who have names such as those 3 above. found this answer : https://stackoverflow.com/a/5333281/169277 For using like but I couldn't figure out how to pass array of values instead of single values. any ideas? UPDATE : I need like because people have two names

Original source

Related problems