title: Rails ActiveRecord fire action after after_save method is done
body: <p>In my Rails 4 Postgres applicaiton I have a Project model and a WorkingDay model with a has_many relationship between them:</p>
  
  <p><code>working_day.rb</code></p>
  
  <pre><code>class WorkingDay &gt; ActiveRecord::Base
    # working_times: text that is parsed into yml json format
  
    belongs_to :project
  end 
  </code></pre>
  
  <p>and <code>project.rb</code></p>
  
  <pre><code>class Project &gt; ActiveRecord::Base
  
    has_many :working_days
  
    # total_cost: decimal based on calculations of working_times of each working_day
  
    before_save :calculate_total_cost
  
    def calculate_total_cost
      # carry out certain calculations based on working_days of this project
      self
    end 
  
    def regen_working_days(wd_json_data)
      self.working_days.each(&amp;:destroy)
      wd_json_data.each do |wd_data|
        self.working_days.create(wd_data)
      end 
      self.save!
      self
    end 
  end  
  </code></pre>
  
  <p>The issue I am having is that once the <code>regen_working_days</code> is fired with the correct <code>wd_json_data</code> parameters, the <code>total_cost</code> is not calculated correctly. It is actually calculated based on values of previous <code>working_days</code>, which means that in the instance this method is fired, the new working_day objects are not yet created and the old ones are still there.</p>
  
  <p>Is there any way to make the <code>self.save!</code> part of the <code>regen_working_days</code> method be fired only after all other transactions are finished? Or am I generally wrong in the way I handle nested objects and the transactions associated with them with my approach?</p>