title: Why is my after_save callback stopping my ActiveRecord association from saving properly?
body: <p>When I comment out my <code>after_save</code> call back, my ActiveRecord associations work just fine. In Rails Console, you'd see:</p>
  
  <pre><code>&gt; @report = Report.create :name =&gt; "foo"
  =&gt; #&lt;Report id: 9, name: "foo", created_at: "2013-03-05 09:51:55", updated_at: "2013-03-05 09:51:55"&gt; 
  &gt; @question = @report.questions.create :description =&gt; "bar"
  =&gt; #&lt;Question id: 18, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 09:52:32", updated_at: "2013-03-05 09:52:32", additive: false, instructions: nil&gt; 
  &gt; @report.questions
  =&gt; [#&lt;Question id: 18, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 09:52:32", updated_at: "2013-03-05 09:52:32", additive: false, instructions: nil&gt;] 
  &gt; @question.reports
  =&gt; [#&lt;Report id: 9, name: "foo", created_at: "2013-03-05 09:51:55", updated_at: "2013-03-05 09:51:55"&gt;]
  </code></pre>
  
  <p>However, the associations stop working when I add the following <code>after_save</code> callback to <code>question.rb</code>:</p>
  
  <pre><code>  def create_matching_surveys
      self.reports.each do |report|
        report.reviews.each do |review|
          review.competitors.each do |competitor|
            competitor.surveys.find_or_create_by_question_id(self.id)
          end
        end
      end
    end
  </code></pre>
  
  <p>Then, in Rails Console, you get:</p>
  
  <pre><code>&gt; @report = Report.create :name =&gt; "foo"
  =&gt; #&lt;Report id: 13, name: "foo", created_at: "2013-03-05 10:20:51", updated_at: "2013-03-05 10:20:51"&gt;
  &gt; @question = @report.questions.create :description =&gt; "bar"
  =&gt; #&lt;Question id: 24, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 10:21:02", updated_at: "2013-03-05 10:21:02", additive: false, instructions: nil&gt; 
  &gt; @report.questions
  =&gt; [#&lt;Question id: 24, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 10:21:02", updated_at: "2013-03-05 10:21:02", additive: false, instructions: nil&gt;] 
  &gt; @question.reports
  =&gt; [] 
  </code></pre>
  
  <p>This happens whether or not the report has reviews that have competitors.</p>
  
  <p>The strange thing is I thought the callback was meant to happen after the question was saved? So by rights the association should save too before any of this happens, right?</p>
  
  <p>How do I fix it?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I think I have to call the callback in the right spot in the object's life cycle, but I can't find that spot. Here's why I think this:</p>
  
  <pre><code>&gt; @report = Report.create :name =&gt; "foo"
  =&gt; #&lt;Report id: 20, name: "foo", created_at: "2013-03-05 12:29:35", updated_at: "2013-03-05 12:29:35"&gt; 
  &gt; @question = @report.questions.create :description =&gt; "bar"
  =&gt; #&lt;Question id: 31, standard_id: nil, description: "bar", element_id: nil, condition_id: nil, blueprint_name: nil, blueprint_url: nil, created_at: "2013-03-05 12:30:14", updated_at: "2013-03-05 12:30:14", additive: false, instructions: nil&gt; 
  &gt; @question.reports
   =&gt; [] 
  &gt; @question.update_attributes :description =&gt; "foo"
  =&gt; true 
  &gt; @question.reports
  =&gt; [#&lt;Report id: 20, name: "foo", created_at: "2013-03-05 12:29:35", updated_at: "2013-03-05 12:29:35"&gt;] 
  </code></pre>
  
  <p>BTW, the method is now in question_observer.rb:</p>
  
  <pre><code>class QuestionObserver &lt; ActiveRecord::Observer
  
    def after_save(model)
      model.reload
      model.reports.reload
      model.reports.each do |report|
        report.reviews.each do |review|
          review.competitors.each do |competitor|
            competitor.surveys.find_or_create_by_question_id(model.id)
          end
        end
      end
      return true
    end
  
  end
  </code></pre>