Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

`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.
  • Loading branch information...
commit 73cb0f98289923c8fa0287bf1cc8857664078d43 1 parent a152fd3
@lazyatom lazyatom authored
View
1  activerecord/lib/active_record/persistence.rb
@@ -114,6 +114,7 @@ def becomes(klass)
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
View
8 activerecord/test/cases/base_test.rb
@@ -1761,6 +1761,14 @@ def test_becomes
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
Please sign in to comment.
Something went wrong with that request. Please try again.