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?
1 Answers
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.

- 5,473
- 8
- 30
- 37
-
You make a good point. I am using the [geocoder gem](https://github.com/alexreisner/geocoder) and [this is the site](http://whoseyourlandlord.com/) that I'm working on. The problem with deciding if the query is a location before passing onto ActiveRecord is if a user searches for a college, they may put in Maryland which would match the name "University of Maryland", but is also a location. And if it is determined that Maryland is a location, University of Maryland might not be found depending on the proximity. – nikorablin Oct 26 '12 at 16:40
-
1Ah...then perhaps you want to break out the search functionality so that typing in a partial name brings up an auto-complete for colleges. Such as, starting with "Uni..." or "Maryl..." brings up options including "University of Maryland". OTherwise, I guess you have to assess whether an omni-search box really adds that much more flexibility, given the extra logic (and possible database hits) you have to add. – Zando Oct 26 '12 at 19:16