title: Correct callbacks for geocoder validation
body: <p>I hope this is not a dupe but since I haven't found a satisfying answer so far I'm gonna put my question. I'm a rails beginner so please be gentle on me ;)</p>
  
  <p>For a small mapping project we're using geocoder gem. We have a model <code>Place</code> containing <code>:street</code>, <code>:house_number</code>, <code>:postal_code</code>, <code>:city</code> wrapped up as a simple string by <code>:address</code> as the virtual attribute which is being passed to geocoder. We're doing the querying using nominatim (-> OSM data). Unlike the google API nominatim returns nil if the given address does not correspond to a lat/lon pair. So far our model file looks like this</p>
  
  <pre><code>class Place &lt; ActiveRecord::Base
    has_many :descriptions, :dependent =&gt; :delete_all
    accepts_nested_attributes_for :descriptions
  
    geocoded_by :address
    before_validation :geocode
  
    validates :address, presence: true
    validates :name, presence: true
    validates :longitude, presence: true,
                          numericality: { less_than_or_equal_to: 90, greater_than_or_equal_to: -90 }
    validates :latitude, presence: true,
                         numericality: { less_than_or_equal_to: 180, greater_than_or_equal_to: -180 }
    validates :categories, presence: true
  
    def address
      ["#{self.street} #{self.house_number}", self.postal_code, self.city].join(', ')
    end
  end
  </code></pre>
  
  <p>Unlike most of the examples I've looked at I'm using <code>before_validation :geocode</code> because otherwise lat and lon would be nil before they become evaluated raising a validation error (not present).
  So far this works for creating new places but I'm unable to modify the validation to make it work for the <code>update</code> action inside my controller. Basically I want to check if the changes applied to <code>address</code> are valid (i.e. whether the API finds a lat/lon pair to the given address). Studying the documentation i found, that i do a conditional callback like <code>before_validate :geocode, if: :address_changed?</code> but since the parameters of the update request are not available inside my model I have no clue how to check that. I'd be glad on any hint!</p>
  
  <p>edit:</p>
  
  <p>Accepted answer below is right, wasn't working instantly because inside my controller I was using <code>update</code> instead of <code>update_attributes</code>.</p>