title: Rails: validate number of associations
body: <p>How do you write validations for a number of associations that is externally defined? I've so far written something like this:</p>
  
  <pre><code>class Document
    validate :publication_count
  
    private
  
    def publication_count
      if publications.count &gt; template.component_count
        errors.add(:articles, 'too many')
      elsif publications.count &lt; template.component_count
        errors.add(:articles, 'not enough')
      end
    end
  </code></pre>
  
  <p>Both <code>publications</code> and <code>template</code> are associations. I just get a rollback error with this code, even though the record should be valid.</p>