pri*_*kha 2 validation geocoding ruby-on-rails ruby-on-rails-3 gmaps4rails
我在当前项目中使用gmaps4rails非常棒的gem,并且必须在DB中导入超过一千个应该充当gmappable的对象.
然而,并非所有人都有正确的地址!因此,当我尝试导入时,在第一个无法使用Gmaps4rails :: GeocodeStatus进行地理编码的地址上失败.
是否有可能在可能的情况下跳过错误和地理编码?
我想出了这个:
acts_as_gmappable :process_geocoding => false
before_save :prepare_gmaps
private
def prepare_gmaps
begin
data = Gmaps4rails.geocode(address).first
self.latitude= data[:lat]
self.longitude= data[:lng]
rescue Gmaps4rails::GeocodeStatus
end
end
Run Code Online (Sandbox Code Playgroud)