diff options
author | James Adam <james@lazyatom.com> | 2011-10-26 12:51:38 +0100 |
---|---|---|
committer | James Adam <james@lazyatom.com> | 2011-11-16 23:19:58 +0000 |
commit | 73cb0f98289923c8fa0287bf1cc8857664078d43 (patch) | |
tree | 0100820aa72a8bc5e80bd3f0cb6c2a9826ac04a7 /activerecord | |
parent | a152fd34d3eb0e4980c4a44dd1f71510890c5417 (diff) | |
download | rails-73cb0f98289923c8fa0287bf1cc8857664078d43.tar.gz rails-73cb0f98289923c8fa0287bf1cc8857664078d43.tar.bz2 rails-73cb0f98289923c8fa0287bf1cc8857664078d43.zip |
`ActiveRecord::Base#becomes` should retain the errors of the original object.
This commit contains a simple failing test that demonstrates the behaviour we expect, and a fix. When using `becomes` to transform the type of an object, it should retain any error information that was present on the original instance.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 1 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 8 |
2 files changed, 9 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 5e65e46a7d..f047a1d9fa 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -114,6 +114,7 @@ module ActiveRecord became.instance_variable_set("@attributes_cache", @attributes_cache) became.instance_variable_set("@new_record", new_record?) became.instance_variable_set("@destroyed", destroyed?) + became.instance_variable_set("@errors", errors) became.type = klass.name unless self.class.descends_from_active_record? became end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index fdb656fe13..997c9e7e9d 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1761,6 +1761,14 @@ class BasicsTest < ActiveRecord::TestCase assert_equal "The First Topic", topics(:first).becomes(Reply).title end + def test_becomes_includes_errors + company = Company.new(:name => nil) + assert !company.valid? + original_errors = company.errors + client = company.becomes(Client) + assert_equal original_errors, client.errors + end + def test_silence_sets_log_level_to_error_in_block original_logger = ActiveRecord::Base.logger log = StringIO.new |