Question

Is there a way I could search with both a where condition and a near condition at the same time, showing results for both. Basically, I want a search bar where a user could enter a name of a person, but they could also enter a location and the search would show results that match both. I tried having the where query and then a separate near query and looping through the Geocoded results and adding them to the main results, but this creates repeats and is less than desirable. Any ideas?

Was it helpful?

Solution

Which geocoding gem are you using? My guess is that instead of doing query.where.near you can do a query.where where you set the equivalent options that the near selector generates.

This is a different question and one theoretically orthogonal to what you asked...but is your controller making any decisions before the query is passed onto ActiveRecord? For example, if a user types in New York, NY, does your controller "guess" that it is a location? Or does it just try to do a name-matching search AND a geocode attempt, no matter what the input is? I ask this because if you do parse out the input, you could have this be an if/else situation, where if query is a location, do a geocode or else do a search by name.

I wonder whether it's sensible design to have a search box that doesn't distinguish between a person's name and a location name before hitting the database, but maybe you have a creative use-case.

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