here company model has geocoding. basically, want make sure add_full_address method called before geocoding method because geocoding method depends on full_address. how work? validations run in order written? need them both happen before validations run because want verify latitude , longitude columns populated because otherwise... want save fail.
class company < activerecord::base include activemodel::dirty validates :name, :organization, :title, :state, :city, presence: true validates :email, presence: true, length: { maximum: 255 }, format: { with: /\a[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i, } validates :latitude , numericality: { greater_than_or_equal_to: -90, less_than_or_equal_to: 90 } validates :longitude, numericality: { greater_than_or_equal_to: -180, less_than_or_equal_to: 180 } before_validation :add_full_address before_validation :geocode, if: ->(obj){(obj.city_changed? || obj.state_changed?)} geocoded_by :full_address |obj, results| if geo = results.first obj.latitude = geo.latitude obj.longitude = geo.longitude end end def add_full_address self.full_address = "#{city}, #{state}" end def d3_coordinates slice(:longitude, :latitude) end end
take @ this: activerecord::callbacks give callbacks can use. ruby being synchronous language, run top bottom, of course callbacks run before validations in case.