When migrating from Rails 2 to Rails 3, validation errors were transferred from ActiveRecord :: Error to ActiveModel :: Errors.
On rails 2, the validation error was of type and message (by the way), and you could check the type of validation error by doing something like the following:
rescue ActiveRecord::RecordInvalid => e
e.record.errors.each do |attr, error|
if error.type == :foo
do_something
end
end
end
But with Rails 3, it seems that everything except the invalid attribute and message is lost. As a result, the only way to determine the type is to compare the error message:
rescue ActiveRecord::RecordInvalid => e
e.record.errors.each do |attr, error|
if error == "foobar"
do_something
end
end
end
Which is not at all ideal (for example, what if you have several validations that use the same message?).
Question:
Is there a better way in rails 3.0 to determine the type of validation error?
source
share