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
errors
is just a plain hash, with the key represents the attribute which has an error, and the value represents the error message. So technically your requirement is doable by replacing the text message with a hash. But the downside is you may need to do more things to show the errors in new format.
For example, use a custom validator to add error code
class Foo < ActiveRecord::Base
attr_accessible :msiisnd
validate :msiisdn_can_not_be_blank
def msiisdn_can_not_be_blank
if msiisdn.blank?
errors.add(:msiisdn, {code: 101, message: "cannot be blank"})
end
end
end
Then use it
foo = Foo.new
foo.errors.count
#=> 0
foo.valid?
#=> false
foo.errors.count
#=> 1
foo.errors[:msiisdn]
#=> [{ code: 101, message: "can not be blank"}]
foo.errors[:msiisdn][0][:code]
#=> 101
So you can use it. But you need to do more work when you need to show the errors correctly, say displaying errors in a form, as this is not a convention.