Skip to content

Commit 120abd1

Browse files
authored
Merge pull request rails#46613 from ghiculescu/strict-loading-build
Fix: Strict Loading error after initializing association owner with primary key
2 parents 4b94070 + 2db098c commit 120abd1

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def build(attributes = nil, &block)
119119
def concat(*records)
120120
records = records.flatten
121121
if owner.new_record?
122-
load_target
122+
loaded!
123123
concat_records(records)
124124
else
125125
transaction { concat_records(records) }

activerecord/test/cases/associations/join_model_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def test_associating_unsaved_records_with_has_many_through
478478
new_tag = Tag.new(name: "new")
479479

480480
saved_post.tags << new_tag
481-
assert new_tag.persisted? # consistent with habtm!
481+
assert_predicate new_tag, :persisted? # consistent with habtm!
482482
assert_predicate saved_post, :persisted?
483483
assert_includes saved_post.tags, new_tag
484484

activerecord/test/cases/associations_test.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,20 @@ def test_push_followed_by_save_does_not_load_target
157157

158158
def test_push_does_not_lose_additions_to_new_record
159159
josh = Author.new(name: "Josh")
160-
josh.posts << Post.new(title: "New on Edge", body: "More cool stuff!")
160+
new_post = Post.new(title: "New on Edge", body: "More cool stuff!")
161+
josh.posts << new_post
161162
assert_predicate josh.posts, :loaded?
162163
assert_equal 1, josh.posts.size
164+
assert_includes josh.posts, new_post
163165
end
164166

165167
def test_append_behaves_like_push
166168
josh = Author.new(name: "Josh")
167-
josh.posts.append Post.new(title: "New on Edge", body: "More cool stuff!")
169+
new_post = Post.new(title: "New on Edge", body: "More cool stuff!")
170+
josh.posts.append new_post
168171
assert_predicate josh.posts, :loaded?
169172
assert_equal 1, josh.posts.size
173+
assert_includes josh.posts, new_post
170174
end
171175

172176
def test_prepend_is_not_defined

activerecord/test/cases/strict_loading_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,33 @@ def test_strict_loading_with_reflection_is_ignored_in_validation_context
162162
end
163163
end
164164

165+
def test_strict_loading_on_concat_is_ignored
166+
developer = Developer.first
167+
developer.strict_loading!
168+
169+
assert_nothing_raised do
170+
developer.audit_logs << AuditLog.new(message: "message")
171+
end
172+
end
173+
174+
def test_strict_loading_with_new_record_on_concat_is_ignored
175+
developer = Developer.new(id: Developer.first.id)
176+
developer.strict_loading!
177+
178+
assert_nothing_raised do
179+
developer.audit_logs << AuditLog.new(message: "message")
180+
end
181+
end
182+
183+
def test_strict_loading_with_new_record_on_build_is_ignored
184+
developer = Developer.new(id: Developer.first.id)
185+
developer.strict_loading!
186+
187+
assert_nothing_raised do
188+
developer.audit_logs.build(message: "message")
189+
end
190+
end
191+
165192
def test_strict_loading_has_one_reload
166193
with_strict_loading_by_default(Developer) do
167194
ship = Ship.create!(developer: Developer.first, name: "The Great Ship")

0 commit comments

Comments
 (0)