title: ActiveRecord callback on column change
body: <p>I have the following relationship model: </p>
  
  <pre><code>class Doctor
    has_many :appointmets
    # has attribute 'average_rating' (float)
  end
  
  class Appointment
    belongs_to :doctor
    # has_attribute 'rating' integer
  end
  </code></pre>
  
  <p>I want to set a callback to set <code>doctor.average_rating</code> each time an appointment gets rated, i.e. each time <code>rating</code> column on Appointments table gets touched.</p>
  
  <p>I tried with this:</p>
  
  <pre><code>class Appointment
    after_update :update_dentist_average_rating, if: :rating_changed?
    # also tried with after_save 
  private
  
    def update_dentist_average_rating
      ratings = Appointment.where(dentist_id: dentist.id).map(&amp;:rating)
      doctor.average_rating = ratings.reduce(:+) / ratings.size if ratings.any?
      doctor.save
    end
  end
  </code></pre>
  
  <p>But doesn't work, <code>appointment.dentist.average_rating</code> returns <code>nil</code> always. I can't seem to test whether the callback is running on <code>appointment.save</code>. <strong>What I'm missing?</strong></p>
  
  <p><strong>Edit:</strong> this is the app flow</p>
  
  <ol>
  <li>A user(of class Patient) saves an appointment with rating: nil and some doctor_id.  </li>
  <li>If the appointment.date is past, appointment.rating can be edited or set on a form via ''</li>
  <li>Then I need to update appointment.doctor.average_rating </li>
  </ol>