diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index c6535082d3a7b3bdc81430348b68ddebcbd396ce..5cd8f77f0d03781a1bfbd5938453d44140e5d6b8 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -78,6 +78,11 @@ def initialize(base) @messages = ActiveSupport::OrderedHash.new end + def initialize_dup(other) + @messages = other.messages.dup + super + end + # Clear the messages def clear messages.clear @@ -118,7 +123,7 @@ def [](attribute) # p.errors[:name] = "must be set" # p.errors[:name] # => ['must be set'] def []=(attribute, error) - self[attribute.to_sym] << error + self[attribute] << error end # Iterates through each error key, value pair in the error messages hash. diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 4edeece3e8c6bf986fa98eca4d3d54d3f446fc00..ab80f193b6b71e52b57b68df24e3df130691b549 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -40,6 +40,14 @@ def test_include? assert errors.include?(:foo), 'errors should include :foo' end + def test_dup + errors = ActiveModel::Errors.new(self) + errors[:foo] = 'bar' + errors_dup = errors.dup + errors_dup[:bar] = 'omg' + assert_not_same errors_dup.messages, errors.messages + end + def test_has_key? errors = ActiveModel::Errors.new(self) errors[:foo] = 'omg'