【问题标题】:How to Geocode address from multiple models?如何从多个模型中对地址进行地理编码?
【发布时间】:2015-12-14 18:51:19
【问题描述】:

我需要将geocode用户的地址转换成纬度和经度以存储在数据库中。

地址表

country_id: int
state_id: int
local_address: string
latitude: float
longitude: float

我有单独的countries and states 表。 所以,要获得 lat & long 值,我需要这样的东西:

geocoded_by :address 
def address
  [local_address, state, country].compact.join(', ')
end

但问题是,这是在模型(地址类)中完成的,我需要访问其他表:国家和州。

所以,想不出办法。任何工作建议都表示赞赏。

【问题讨论】:

    标签: ruby-on-rails ruby model geocoding rails-geocoder


    【解决方案1】:

    在您的模型中,您需要关联到您的国家和州表 我假设您正在保存 state id 和 country_id

    class User < ActiveRecord::Base
      belongs_to :state
      belongs_to :country
    
      geocoded_by :address 
      def address
        [local_address, state.name, country.name].compact.join(', ')
      end
    end
    

    【讨论】:

    • 是的!有效!!它需要进行正确的关联并使用“Model.attribute_name”来完全加入单个地址。感谢您这么快的回复。
    • 不是问题,我们都去过那里,我们需要另一双眼睛
    猜你喜欢
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    相关资源
    最近更新 更多