ruby-on-rails - 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?
解决方案
正如您在问题中提到的,您已经在用户表中拥有名为“城市”的字段。
(参考:https ://github.com/alexreisner/geocoder#geocoding-objects )
尝试这个:
SpreeUser.near('Austin', 200, units: :km) # users within 200 kms of Austin city
推荐阅读
- reactjs - 无法在生产中导入 babel-plugins/presets 以进行转换
- javascript - 显示上下文菜单(假);仅当您在右键菜单外单击时
- javascript - Mongoose.js:查找所有喜欢 Cat 帖子的用户
- python - 查找超过 2 天的日期并将其替换为变量 - Python
- html - 如何从带有 xml 数据文件的单选按钮中获取值?
- python - 如何修复无法在python中解包不可迭代的错误
- android - AR 应用程序不工作显示错误“无法创建 AR 会话”
- node.js - Express-Session - 有没有办法在 ExpressJS 路由之外的会话中存储数据?
- php - 静态文件的问题 - 使用 nginx 在子目录下托管 PHP 应用程序
- ios - 为什么 iOS 13.4 之后不执行 UIApplication 的 next 方法?