Question

I'm transitioning an application written in Rails-2.3 with SearchLogic to Rails-3.0 with Arel and MetaWhere, and I'm running into an operation that I don't know how the write.

The old code was:

if params[:city] && params[:city].respond_to?(:each)
  users = users.person_address_city_like_any(params[:city])
end

what this did was run a LIKE match against each item in the params[:city] array.

This is easy enough in MetaWhere when there's only one search term:

users = users.where(:person => { :address => { :city.matches => '%city1%' } })

but how would I write this with an arbitrary number of cities?

Was it helpful?

Solution

Try:

users = users.where(:person => { :address => { :city.matches_any => ['%city1%','%city2%'] } })

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top