Add ActiveRecord Error Not Tied To Any Attribute
Often the errors on an ActiveRecord object are tied to a specific attribute of that object. For instance, when this validation is violated
Then the error will be tied to :name
.
With the ActiveModel::Errors#add
method, we can write custom validation logic that ties an error to a specific attribute.
Errors don't have to be tied to specific attribute. They can be tied to the object as a whole. This can be better for validations, like the one above, that involve multiple attributes.
By using the :base
symbol, we are ascribing this error to the object as a whole.
Last updated