rails/rails — pull request #3369 by martinsvalin (2011-10-19 17:43:04+00:00)
Title: New #added? method on ActiveModel::Errors

The `#added?` method makes it possible to check if a specific error has been added, using the same parameters as for `#add`.

This is useful if you need different behaviour depending on what errors are present on an object. My use case was when adding email addresses to a list. Some errors are permanent (e.g. already subscribed) while other errors are not permanent (e.g. invalid formatting), so the user can be given a chance to remedy the error.

I had a mix of errors on the same attribute, so I could not simply check for the presence of errors on that attribute. Checking for key-value-pairs was also made difficult, as my app is translated into five languages.

I ended up monkey patching ActiveModel::Errors, as have others:
http://stackoverflow.com/questions/6826820/default-activerecord-activemodel-errors-are-anonymous
http://stackoverflow.com/questions/3671098/better-way-to-access-individual-rails-activerecord-error

I'm not an experienced Rails contributor, so if you have comments on coding style, please let me know. I'll be happy to make modifications.