Find an ActiveRecord::Error in an object by attribute name and error type.

Is there a way to know if a record has an error with a certain attribute and error type after validating it? I was wondering if there is a way to accomplish this task by using something similar as the ActiveModel::Errors.get method but passing two arguments instead of one. For instance:

ActiveModel::Errors.get(:user_id, :blank) # => ["can't be blank"]

If this is not implemented yet, I’d like to help with these changes. Thanks!

You should be able to use the #added? method for this.

Hope that helps.

Damn! I didn’t see that one. Thanks Carlos.

I tried to use added? but it seems that if the returned value is false, it adds the consulted attribute to the errors’ hash and sets its value to an empty array. This produces an error (at least in my case) RuntimeError: can't add a new key into hash during iteration. Do you know the why of this behaviour?