I have an Address model which contains the fields: number, street, city, post_code
.
I use a method called fulladdress
to interpolate all the fields into one, which is then passed to geocoder which returns the Longitude and Latitude for the address.
Here is my Address model:
class Address < ActiveRecord::Base
belongs_to :user
def fulladdress
"#{number} #{street}, #{city}, #{post_code}"
end
geocoded_by :fulladdress
after_validation :geocode, :if => :number_changed?
end
At the moment, geocoder only updates the Long and Lat if number
has changed. What I want is for geocoder to run if any of number, street, city, post_code
change. What is the best practice way to do this?
I've never worked with geocoder, but if you just want something to update if attributes change you could setup an after_save
callback.
class Address < ActiveRecord::Base
belongs_to :user
def fulladdress
"#{number} #{street}, #{city}, #{post_code}"
end
geocoded_by :fulladdress
after_save :geocode
end
This should force it to update any time the address is updated, which should mean that at least one of the attributes has changed so no need to check if they are different.