File tree 4 files changed +9
-12
lines changed
4 files changed +9
-12
lines changed Original file line number Diff line number Diff line change @@ -211,12 +211,8 @@ def create!(attributes = nil, &block)
211
211
212
212
private
213
213
def find_target
214
- if owner . strict_loading? && owner . validation_context . nil?
215
- Base . strict_loading_violation! ( owner : owner . class , association : klass )
216
- end
217
-
218
- if reflection . strict_loading? && owner . validation_context . nil?
219
- Base . strict_loading_violation! ( owner : owner . class , association : reflection . name )
214
+ if ( owner . strict_loading? || reflection . strict_loading? ) && owner . validation_context . nil?
215
+ Base . strict_loading_violation! ( owner : owner . class , reflection : reflection )
220
216
end
221
217
222
218
scope = self . scope
Original file line number Diff line number Diff line change @@ -277,14 +277,14 @@ def self.allow_unsafe_raw_sql=(value) # :nodoc:
277
277
self . default_role = writing_role
278
278
self . default_shard = :default
279
279
280
- def self . strict_loading_violation! ( owner :, association :) # :nodoc:
280
+ def self . strict_loading_violation! ( owner :, reflection :) # :nodoc:
281
281
case action_on_strict_loading_violation
282
282
when :raise
283
- message = "`#{ association } ` called on `#{ owner } ` is marked for strict_loading and cannot be lazily loaded."
283
+ message = "`#{ owner } ` is marked for strict_loading. The `#{ reflection . klass } ` association named `: #{ reflection . name } ` cannot be lazily loaded."
284
284
raise ActiveRecord ::StrictLoadingViolationError . new ( message )
285
285
when :log
286
286
name = "strict_loading_violation.active_record"
287
- ActiveSupport ::Notifications . instrument ( name , owner : owner , association : association )
287
+ ActiveSupport ::Notifications . instrument ( name , owner : owner , reflection : reflection )
288
288
end
289
289
end
290
290
end
Original file line number Diff line number Diff line change @@ -22,9 +22,10 @@ def self.reset_runtime
22
22
def strict_loading_violation ( event )
23
23
debug do
24
24
owner = event . payload [ :owner ]
25
- association = event . payload [ :association ]
25
+ association = event . payload [ :reflection ] . klass
26
+ name = event . payload [ :reflection ] . name
26
27
27
- color ( "Strict loading violation: #{ association } lazily loaded on #{ owner } ." , RED )
28
+ color ( "Strict loading violation: #{ owner } is marked for strict loading. The #{ association } association named : #{ name } cannot be lazily loaded ." , RED )
28
29
end
29
30
end
30
31
Original file line number Diff line number Diff line change @@ -407,7 +407,7 @@ def test_strict_loading_violation_can_log_instead_of_raise
407
407
developer . strict_loading!
408
408
assert_predicate developer , :strict_loading?
409
409
410
- assert_logged ( "Strict loading violation: AuditLog lazily loaded on Developer ." ) do
410
+ assert_logged ( "Strict loading violation: Developer is marked for strict loading. The AuditLog association named :audit_logs cannot be lazily loaded ." ) do
411
411
developer . audit_logs . to_a
412
412
end
413
413
ensure
You can’t perform that action at this time.
0 commit comments