From fb665217723f7c4e9e96bb7658fc3048a1d64379 Mon Sep 17 00:00:00 2001 From: Christian Seiler Date: Thu, 16 Aug 2012 23:19:47 +0200 Subject: [PATCH] Call super to nullify the reference to the original errors object in the dup'ed object (call ActiveModel::Validations#initialize_dup). Closes #7291 --- activerecord/CHANGELOG.md | 5 +++++ activerecord/lib/active_record/timestamp.rb | 1 + activerecord/test/cases/dup_test.rb | 14 ++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 9abfe2e6fd049..6e0f1f31dd404 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -269,6 +269,11 @@ *Ari Pollak* +* Fix AR#dup to nullify the validation errors in the dup'ed object. Previously the original + and the dup'ed object shared the same errors. + + * Christian Seiler* + * Raise `ArgumentError` if list of attributes to change is empty in `update_all`. *Roman Shatsov* diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb index bf95ccb2985e9..dd08a6f4f5e12 100644 --- a/activerecord/lib/active_record/timestamp.rb +++ b/activerecord/lib/active_record/timestamp.rb @@ -42,6 +42,7 @@ module Timestamp def initialize_dup(other) # :nodoc: clear_timestamp_attributes + super end private diff --git a/activerecord/test/cases/dup_test.rb b/activerecord/test/cases/dup_test.rb index 71b2b16608129..4e2adff344624 100644 --- a/activerecord/test/cases/dup_test.rb +++ b/activerecord/test/cases/dup_test.rb @@ -107,5 +107,19 @@ def test_dup_after_initialize_callbacks assert Topic.after_initialize_called 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 end