Skip to content

Commit b0ed7ac

Browse files
authored
Merge pull request #51627 from shes50103/strict-loading-not-raising-when-pluck
[Fix #51524] Fix `strict_loading` violations ignored when using `pluck`
2 parents 18f0be3 + 36525f8 commit b0ed7ac

4 files changed

Lines changed: 39 additions & 10 deletions

File tree

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix `strict_loading` violations ignored when using `pluck`
2+
3+
*Johnson Chan*
4+
15
* Move the defaulting of `prevent_writes` to `true` when using the `reading` role into the parameters
26
of the role switching methods, and raise an `ArgumentError` if `prevent_writes: false` is provided
37
with the `reading` role.

activerecord/lib/active_record/associations/association.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ def create!(attributes = nil, &block)
232232
_create_record(attributes, true, &block)
233233
end
234234

235+
def violates_strict_loading?
236+
return unless find_target?
237+
238+
return if @skip_strict_loading
239+
240+
return unless owner.validation_context.nil?
241+
242+
return reflection.strict_loading? if reflection.options.key?(:strict_loading)
243+
244+
owner.strict_loading? && !owner.strict_loading_n_plus_one_only?
245+
end
246+
235247
# Whether the association represents a single record
236248
# or a collection of records.
237249
def collection?
@@ -281,16 +293,6 @@ def skip_strict_loading(&block)
281293
@skip_strict_loading = skip_strict_loading_was
282294
end
283295

284-
def violates_strict_loading?
285-
return if @skip_strict_loading
286-
287-
return unless owner.validation_context.nil?
288-
289-
return reflection.strict_loading? if reflection.options.key?(:strict_loading)
290-
291-
owner.strict_loading? && !owner.strict_loading_n_plus_one_only?
292-
end
293-
294296
# The scope for this association.
295297
#
296298
# Note that the association_scope is merged into the target_scope only when the

activerecord/lib/active_record/associations/collection_proxy.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,10 @@ def calculate(operation, column_name)
726726
end
727727

728728
def pluck(*column_names)
729+
if proxy_association.violates_strict_loading?
730+
Base.strict_loading_violation!(owner: proxy_association.owner.class, reflection: proxy_association.reflection)
731+
end
732+
729733
null_scope? ? scope.pluck(*column_names) : super
730734
end
731735

activerecord/test/cases/strict_loading_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,25 @@ def test_strict_loading_with_has_one_through_does_not_prevent_creation_of_associ
355355
end
356356
end
357357

358+
def test_on_lazy_loading_and_strict_loading_with_pluck
359+
with_strict_loading_by_default(Developer) do
360+
dev = Developer.first
361+
362+
assert_raises ActiveRecord::StrictLoadingViolationError do
363+
dev.audit_logs.pluck(:id)
364+
end
365+
end
366+
end
367+
368+
def test_preload_audit_logs_are_strict_loading_with_pluck
369+
with_strict_loading_by_default(Developer) do
370+
dev = Developer.preload(:audit_logs).first
371+
372+
assert_nothing_raised do
373+
dev.audit_logs.pluck(:id)
374+
end
375+
end
376+
end
358377

359378
def test_preload_audit_logs_are_strict_loading_because_parent_is_strict_loading
360379
developer = Developer.first

0 commit comments

Comments
 (0)