Oviron Oviron - 2 years ago 91
Ruby Question

Activerecord query against array column using wildcard

So let's say i have a

model with array column
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
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 Source

Can you try this

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

I believe this will work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download