ruby-on-railspostgresqlactiverecordgeolocationrails-geocoder

Ruby On Rails - Geocoder - Near with condition


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!


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))