Oviron Oviron - 1 month ago 5
Ruby Question

Activerecord query against array column using wildcard

So let's say i have a

Customer
model with array column
phones
.
It's pretty easy to find all customers with given phone

Customer.where('? = ANY(phones)', '+79851234567')


But i can't figure out how to use
LIKE
with wildcard when i want to find customers with phones similar to given one, something like:

Customer.where('ANY(phones) LIKE ?', '+7985%')


I'm using PostgreSQL 9.5 and Rais 4.2

Any ideas?

Answer

Can you try this

Customer.where("array_to_string(phones, ', ') like ?", '+7985%')

I believe this will work.