Ruby On Rails - Geocoder - Near with condition

落爺英雄遲暮 提交于 2019-12-04 14:10:32

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/15056333/ruby-on-rails-geocoder-near-with-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!