【问题标题】:How to use two locations with Geocoder rails如何将两个位置与地理编码器导轨一起使用
【发布时间】:2015-10-24 17:08:19
【问题描述】:

我在 Rails 中使用geocoder gem,但我的模型有两个位置(route_from、route_to)。
我只能将它用于一个属性。但我不会同时使用两者。

geocoded_by :address  
after_validation :geocode 


我该怎么办?

【问题讨论】:

  • 你到底有什么问题?是什么让你烦恼?您应该用更多细节来扩展问题。

标签: ruby-on-rails rails-geocoder


【解决方案1】:

Geocoder 并不是真正为在同一模型中处理位置而构建的。与其用锤子敲击,不如将您的域分成两个模型:

class Journey < ActiveRecord::Base
  has_one :route_from, class: 'Waypoint'
  has_one :route_to, class: 'Waypoint'
end

class Waypoint < ActiveRecord::Base
  reverse_geocoded_by :latitude, :longitude
  after_validation :reverse_geocode  # auto-fetch address
end

如果您可以想象路由有两个以上的节点,您可能希望使用连接模型对其进行设置。

【讨论】:

  • 谢谢,但是如何使用这种方式创建新的旅程?
猜你喜欢
  • 1970-01-01
  • 2021-01-01
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 2012-01-06
  • 2011-11-27
  • 1970-01-01
相关资源
最近更新 更多