I'm performing an operation on a base_user
. I want my @user
's errors to be the same as the base_user
's because I end up showing those errors as a banner in the view. But, I don't know how to set the @user
's errors. Here is what I'm doing:
base_user.validate_password_change(params)
if base_user.errors.size > 0
#@user.errors.initialize_dup(base_user.errors) # does not work
@user.errors = base_user.errors.dup # does not work
raise 'Validation Errors' if @user.errors.size > 0
end
@user.errors
is an empty hash. base_user.errors.messages is a hash containing an array named password with elements (strings of reasons why password is not properly formatted). According to the ActiveResource API, ActiveResource::Error objects can do from_array
, from_hash
(not listed in API but I see it in code), from_json
, and from_xml
, but it doesn't have an initialize or setter.
I'm using Ruby 2.0 and Rails 4.0.0, which uses ActiveResource 4
ActiveResource::Errors inherits from ActiveModel::Errors.
You can overwrite the errors on the @user
instance by enumerating the attributes of base_user
:
base_user.errors.each do |name, value|
@user.errors.set(name, value)
end
You may need to consider the case where you have errors on both objects, and overwriting one with the other might not be appropriate.