Using Geocoder, is there a way to keep the street name, city, and zip code to separate the columns?

I am trying to switch my application to using geocoder. In my table places, I have columns for the address lat, lng, street_address, city and zip. Using a geocoder, I can happily fill in the lat, lng and address columns after checking with the next model in my places

attr_accessible :address, :lat, :lng    
geocoded_by :address, :latitude  => :lat, :longitude => :lng
after_validation :geocode, :if => :address_changed? 

Is there a way for the geocoder to add the street name, city, and zip code to three other separate columns?

+5
source share
1 answer

I'm still new to rails, so I skipped this first, but hope this helps someone else.

in my model

geocoded_by :address  do |obj,results|
  if geo = results.first
    obj.city    = geo.city
    obj.lat = geo.latitude
    obj.lng = geo.longitude
    obj.zip = geo.postal_code
    obj.state = geo.state
    obj.country = geo.country_code
  end
end

and in my opinion

 @tonic.address = params[:address]
+10
source

All Articles