title: Rails3 Cocoon Validate Nested Field Count
body: <p>I'm having issues validating with cocoon and the number of fields a model is allowed. Using cocoon, rails3, I have a nested form whereby my locations have many links.</p>
  
  <p>I need to restrict the number of links each location has to 5. </p>
  
  <p>In my location.rb model, I have this:</p>
  
  <pre><code> class Location &lt; ActiveRecord::Base
  
     has_many :links
     accepts_nested_attributes_for :links, :reject_if =&gt; lambda { |a| a[:link_name].blank? }, :allow_destroy =&gt; true   
     validate :check_link_count
  
     ...
  
     def check_link_count
        if self.links.count &gt; 5
          self.errors.add :base, "No more than 5 links allowed."
        end
     end
  
     ...
  </code></pre>
  
  <p>Adding up to 5 links, all works fine. </p>
  
  <p>If I add 6 links and save, I get an error. Also good.</p>
  
  <p>The problem is when I try and remove the links - it would seem the link is only removed after the save (I think). If I therefore remove all fields, I still get an error.</p>
  
  <p>Any suggestions? Is there another way to validate?</p>