Ca35abdcc0ebbaada3c935c2746ed5ea

I simply want to overwrite the zipcode= attributes so that it only does the lookup on the geocoder if the zipcode changed.
All values are saved from the update_geolocation and nilify_geolocation operations but I am not able to save the new_zipcode. I have tried various tricks to prevent an endless loop and didnt manage do achieve anything good and elegant. I then tried the solution presented here which doesnt work either. Anyone has an idea?

def zipcode=(new_zipcode)
    if new_zipcode != self.zipcode
      write_attribute(zipcode, new_zipcode) #problem here
      #tries to geolocate with the zipcode then does the corresponding update
      GeoKit::Geocoders::MultiGeocoder.geocode(new_zipcode).success? update_geolocation(loc) : nilify_geolocation
    end
  end

Refactorings

No refactoring yet !

4d1c9dad17af98e55cb65b4efce27c42

Ben Burkert

November 28, 2007, November 28, 2007 03:23, permalink

2 ratings. Login to rate!

the first argument of write_attribute should be a symbol or a string. In your example you are trying to write the new value of zipcode to a column with the same name as the old value of zip code.

def zipcode=(new_zipcode)
    if new_zipcode != self.zipcode
      write_attribute(:zipcode, new_zipcode) #problem here
      #tries to geolocate with the zipcode then does the corresponding update
      GeoKit::Geocoders::MultiGeocoder.geocode(new_zipcode).success? update_geolocation(loc) : nilify_geolocation
    end
  end

Your refactoring





Format Copy from initial code

or Cancel