Created
September 17, 2009 10:52
-
-
Save jcf/188447 to your computer and use it in GitHub Desktop.
Copy errors on an ActiveRecord object in to another instance's errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cat, dog = Cat.new, Dog.new | |
# cat.friends << dog | |
# cat.save | |
# => false | |
# cat.errors | |
# #<ActiveRecord::Errors ... @errors={"dog" => "is not valid"}> | |
# cat.errors << dog.errors | |
# cat.errors | |
# #<ActiveRecord::Errors ... @errors={"dog" => "is not valid", "bark" => "is not bigger than bite"} | |
class ActiveRecord::Errors | |
def <<(other_errors) | |
@errors.merge!(other_errors.instance_variable_get(:@errors)) | |
end | |
end |
Clarification. Use the errors from the 'other' object as the argument:
person.errors.copy!(other_object.errors)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
copy!(other) public
Copies the errors from other.
other - The ActiveModel::Errors instance.
Examples
source: https://apidock.com/rails/v5.2.3/ActiveModel/Errors/copy%21