ruby-on-railsmodelattributescontrollergeocode

Rails geocoder attribute


In the rails is it possible to make a function that everytime a particular attribute is changed, rails executes a function. In my case, everytime object.address (I have geocoded_by :address and after_validation :geocode) is changed, I want to do object.geocode. I am on rails 3


Solution

  • The best solution, I think would be to include ActiveModel::Dirty in your model, then you can check it in a callback

    class MyModel < ActiveRecord::Base
      def after_save
         if address_changed? do
           #some stuff..
         end
      end
    end
    

    see ActiveModel::Dirty