diff options
-rw-r--r-- | activemodel/lib/active_model/validations.rb | 6 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 15 |
2 files changed, 21 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 3ed72bae3b..72176e9bdd 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -177,6 +177,12 @@ module ActiveModel super end end + + # Clean the +Errors+ object if instance is duped + def initialize_dup(other) # :nodoc: + @errors = nil + super + end # Returns the +Errors+ object that holds all information about attribute error messages. def errors diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index a716d0896e..f9bb877316 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -344,4 +344,19 @@ class ValidationsTest < ActiveModel::TestCase Topic.validates :title, options assert_equal({ :presence => true }, options) end + + def test_dup_validity_is_independent + Topic.validates_presence_of :title + topic = Topic.new("title" => "Litterature") + topic.valid? + + duped = topic.dup + duped.title = nil + assert duped.invalid? + + topic.title = nil + duped.title = 'Mathematics' + assert topic.invalid? + assert duped.valid? + end end |