Skip to content

Commit 9a868a0

Browse files
committed
Merge pull request rails#6324 from acapilleri/dup_validation_3_2
Dup validation 3 2
2 parents 007539d + 396e383 commit 9a868a0

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

activemodel/lib/active_model/validations.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ def inherited(base)
165165
end
166166
end
167167

168+
# Clean the +Errors+ object if instance is duped
169+
def initialize_dup(other) # :nodoc:
170+
@errors = nil
171+
super
172+
end
173+
168174
# Returns the +Errors+ object that holds all information about attribute error messages.
169175
def errors
170176
@errors ||= Errors.new(self)

activemodel/test/cases/validations_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,19 @@ def test_strict_validation_error_message
339339
end
340340
assert_equal "Title can't be blank", exception.message
341341
end
342+
343+
def test_dup_validity_is_independent
344+
Topic.validates_presence_of :title
345+
topic = Topic.new("title" => "Litterature")
346+
topic.valid?
347+
348+
duped = topic.dup
349+
duped.title = nil
350+
assert duped.invalid?
351+
352+
topic.title = nil
353+
duped.title = 'Mathematics'
354+
assert topic.invalid?
355+
assert duped.valid?
356+
end
342357
end

0 commit comments

Comments
 (0)