In rails activerecord validation, normally if a validation fails, it will add an error message in the errors attribute of models, however our clients demands an error code be re
Rails 5 will include a similar feature with foo.errors.details
. You can easily use this to build better api errors.
class Person < ActiveRecord::Base
validates :name, presence: true
end
person = Person.new
person.valid?
person.errors.details
# => {name: [{error: :blank}]}
If you prefer code errors as numbers, you can easily match numbers with the error keys.
For Rails 3 & 4 you can use this gem https://github.com/cowbell/active_model-errors_details witch is the exact same feature backported.