Where with like

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)

Found the solution :

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

No need for a block, you can just do:

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