首页 > 解决方案 > How do we get list of users within range of certain city?

问题描述

I have list of users with field city. How do we get list of users who are about 200km from certain city?

I am using geocoder gem to find nearest location. However, how do we do it through city only without lat and lon present on the db for User?

user = User.last    
user.address = 'Sydney'
location = Geocoder.search(user.address).first
lat = location.latitude
lon = location.longitude

within_user = SpreeUser.near([lat, lon], 200, units: :km)

This will give the error because latitude and longitude does not exist for User. Is there a way it can be done through other field, like address in this case?

标签: ruby-on-railsrubyelasticsearch

解决方案


正如您在问题中提到的,您已经在用户表中拥有名为“城市”的字段。

(参考:https ://github.com/alexreisner/geocoder#geocoding-objects )

尝试这个:

SpreeUser.near('Austin', 200, units: :km)      # users within 200 kms of Austin city

推荐阅读