title: Custom validation on nested attributes
body: <p>I need to find a way to get rails (3.2.8) to save nested attributes before performing validation on the parent object. I've searched a long time for an answer to my problem and, while I've found similar questions, I haven't yet seen an elegant solution.</p>
  
  <p>Here's the situation: </p>
  
  <p>I have an activity_log where each log_entry has multiple activities. The log_entry has a field called 'hours', which represents the total time worked that day. Each activity also has an 'hours' field, representing time spent on that activity. I need to make sure that log_entry.activities.sum(:hours) is not greater than log_entry.hours.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>class LogEntry &lt; ActiveRecord::Base
    attr_accessible :date, :hours, :activities_attributes
  
    has_many :activities
    accepts_nested_attributes_for :activities
  
    validate :sum_hours_not_more_than_total_hours
  
    private
    def sum_hours_not_more_than_total_hours
      unless activities.sum(:hours) &lt;= hours
        errors.add(:hours, "Hours cannot exceed total hours")
      end
    end
  end
  </code></pre>
  
  <p>The problem is that it checks against the db when calculating activities.sum(:hours) instead of checking against the new data in params. So if I set a log_entry's hours to 4 and create an activity with hours set to 5, it will save with no errors (because the db says that activities.sum(:hours) == 0). But if I edit that same record and set the activity's hours to 4 it will give an error (because the db says activities.sum(:hours) == 5), when it should pass.</p>
  
  <p>I found a workaround here: <a href="http://railsforum.com/viewtopic.php?id=41562" rel="nofollow">http://railsforum.com/viewtopic.php?id=41562</a>, but I was hoping there was a more elegant way of solving this problem. (Actually, this solution doesn't work for me as described because it assumes that the only problem is that records are passing validation that shouldn't. I also have the problem that records fail validation when they should pass. I imagine I could fix it by skipping the validation step and then doing a check like sum_hours_not_more_than_total_hours in the controller (after save), but I really like the sense of security I get from validation.)</p>