Skip to content

Commit fa96398

Browse files
authored
Merge pull request rails#53398 from kamipo/fix_strict_loading_propagation
Fix strict loading propagation even if statement cache is not used
2 parents e9fa6fa + 1f731f5 commit fa96398

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

activerecord/lib/active_record/association_relation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def _create!(attributes, &block)
4343
def exec_queries
4444
super do |record|
4545
@association.set_inverse_instance_from_queries(record)
46+
@association.set_strict_loading(record)
4647
yield record if block_given?
4748
end
4849
end

activerecord/lib/active_record/associations/association.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ def reset_scope
120120
@association_scope = nil
121121
end
122122

123+
def set_strict_loading(record)
124+
if owner.strict_loading_n_plus_one_only? && reflection.macro == :has_many
125+
record.strict_loading!
126+
else
127+
record.strict_loading!(false, mode: owner.strict_loading_mode)
128+
end
129+
end
130+
123131
# Set the inverse association, if possible
124132
def set_inverse_instance(record)
125133
if inverse = inverse_association_for(record)
@@ -260,11 +268,7 @@ def find_target(async: false)
260268
klass.with_connection do |c|
261269
sc.execute(binds, c, async: async) do |record|
262270
set_inverse_instance(record)
263-
if owner.strict_loading_n_plus_one_only? && reflection.macro == :has_many
264-
record.strict_loading!
265-
else
266-
record.strict_loading!(false, mode: owner.strict_loading_mode)
267-
end
271+
set_strict_loading(record)
268272
end
269273
end
270274
end

activerecord/test/cases/strict_loading_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ def test_strict_loading_n_plus_one_only_mode_with_has_many
5959
assert_raises ActiveRecord::StrictLoadingViolationError do
6060
developer.projects.last.firm
6161
end
62+
63+
assert_nothing_raised do
64+
developer.projects_extended_by_name.to_a
65+
end
66+
67+
assert developer.projects_extended_by_name.all?(&:strict_loading?)
68+
assert_raises ActiveRecord::StrictLoadingViolationError do
69+
developer.projects_extended_by_name.last.firm
70+
end
6271
end
6372

6473
def test_strict_loading_n_plus_one_only_mode_with_belongs_to

0 commit comments

Comments
 (0)