Where with like


#1

I’m trying to search in my table. I found this :

contacts.where { email.ilike('%myemail%') }.to_a

this work fine. But I want to do like this :

contacts.where { "email".ilike('%myemail%') }.to_a 

because my fields are dynamic (the user can choose an other field)


#2

Found the solution :

field = “email”
contacts.where { contacts[field].ilike(’%myemail%’) }.to_a


#3

No need for a block, you can just do:

contacts.where(contacts[field].ilike('%myemail%')).to_a