GAURAV VARMA
Rails 6.1 overhauls the ActiveModel::Errors
API, making it behave more like a true object.
What’s new?
- Errors are now objects instead of just hashes
- Improved error messages and introspection
- Easier to add and query error metadata
Example
1user.errors.add(:email, :invalid, message: "is not a valid format")
2user.errors[:email].any? # true
3user.errors[:email].first.full_message # => "Email is not a valid format"
4user.errors[:email].first.attribute # => :email
5user.errors[:email].first.type # => :invalid
This change improves form error handling by providing more structured error information, simplifies testing error conditions with easier introspection, and enhances serialization by allowing inclusion of error metadata in API responses.
Summary
The new ActiveModel::Errors
API gives developers better tools for working with validations and error messages, leading to more robust and maintainable applications.
For more details, check out the PR #32313.