diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 9c677101f5340..d06267459b995 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* After a successful `reload`, `new_record?` is always false. + + Fixes #12101. + + *Matthew Draper* + * `has_many :through` associations will no longer save the through record twice when added in an `after_create` callback defined before the associations. diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 9d95365e1e830..85aac0e18c05e 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -399,6 +399,7 @@ def reload(options = nil) @column_types = self.class.column_types @column_types_override = fresh_object.instance_variable_get('@column_types_override') @attributes_cache = {} + @new_record = false self end diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index 74d214395b307..10747dff81fcc 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -846,4 +846,16 @@ def test_instantiate_creates_a_new_instance post.body end end + + def test_find_via_reload + post = Post.new + + assert post.new_record? + + post.id = 1 + post.reload + + assert_equal "Welcome to the weblog", post.title + assert_not post.new_record? + end end