Ruby On Rails - Geocoder - Near with condition

activerecord, geolocation, postgresql, rails-geocoder, ruby-on-rails

Solution

Based off of this answer, you should be able to do something like:

near = Spot.near([lat, long], distance)
visited = Spot.where(visited: true)

near = near.where_values.reduce(:and)
visited = visited.where_values.reduce(:and)

Spot.where(near.or(visited))

Problem

I'm using GeoCoder in my application. Now I need to search for objects in my database which are close to a position OR have specific attribute set. I would like to perform this action in one database query, because the database is realy huge. I would like to have something like ``` Spot.near([lat,long],distance).where("visited = ?",true). ``` The distance and the visited attribute should be combined with an OR, not with an AND. Does anyone have an idea how to do this? Thank you!

Original source