title: How can I store extra data with a validation error message in Rails 3?
body: <p>I'm trying to store some additional data along with the standard error message in a custom validator in Rails 3.</p>
  
  <p>For example, (ignoring built-in validators) suppose I wanted to check to see if a post is a duplicate before it's saved. I might write my custom validation method like this:</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
  
    # prevent duplicate posts
    validate do |post|
      duplicates = Post.find_all_by_body(body)
      errors.add_to_base("Post is a duplicate!") if duplicates.length
  
      # something like this is desired:
      # errors.add_to_base("Post is a duplicate",
      #  :extra =&gt; { :duplicates =&gt; duplicates })
    end
  
  end
  </code></pre>
  
  <p>This will let the user know there are duplicates, but along with adding the error message I would also like to store the duplicates so they can be displayed to the user. How would I store the list of duplicate posts retrieved during validation such that it is associated with the record's errors for the <code>body</code> field, and available to my view?</p>
  
  <p>A simpler example might be length validation: If a field exceeds its maximum length, how can I store the maximum length along with an error message without simply interpolating it into the message as Rails currently does?</p>