Rails validation issue with before_validation
- by Chance
I'm still fairly new to rails so I'm not sure what I'm missing here.
I'm using GeoKit to geocode an address upon saving. I have a method that geocodes an address and if it fails to find it, it adds an error to the errors list. I've tested it in the console and it is failing on the geocode (presumably adding the error) but still saving successfully.
acts_as_mappable
before_validation_on_create :geocode_address
before_validation_on_update :geocode_address
validates_presence_of :street
validates_presence_of :city
validates_presence_of :state
validates_presence_of :zip
validates_presence_of :name
validates_uniqueness_of :name
def geocode_address
geo=Geokit::Geocoders::MultiGeocoder.geocode ("#{street}, #{city}, #{state}, #{zip}")
puts "geocoded: #{street}, #{city}, #{state}, #{zip}"
if geo.success
self.lat, self.lng = geo.lat,geo.lng
else
errors.add(:street, "Could not Geocode address")
end
puts "geo status: #{geo.success}"
end
Any help would be greatly appreciated, thanks :)