Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
lazyatom committed Nov 16, 2011
1 parent a152fd3 commit 73cb0f9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions activerecord/lib/active_record/persistence.rb
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 73cb0f9

Please sign in to comment.