Skip to content

Commit 7659dd0

Browse files
authored
Merge pull request rails#41114 from eileencodes/add-association-name-to-strict-loading
Improve `strict_loading` violation error message
2 parents 48f3c3e + d2378bd commit 7659dd0

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

activerecord/lib/active_record/associations/association.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,8 @@ def create!(attributes = nil, &block)
211211

212212
private
213213
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)
220216
end
221217

222218
scope = self.scope

activerecord/lib/active_record/core.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,14 @@ def self.allow_unsafe_raw_sql=(value) # :nodoc:
277277
self.default_role = writing_role
278278
self.default_shard = :default
279279

280-
def self.strict_loading_violation!(owner:, association:) # :nodoc:
280+
def self.strict_loading_violation!(owner:, reflection:) # :nodoc:
281281
case action_on_strict_loading_violation
282282
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."
284284
raise ActiveRecord::StrictLoadingViolationError.new(message)
285285
when :log
286286
name = "strict_loading_violation.active_record"
287-
ActiveSupport::Notifications.instrument(name, owner: owner, association: association)
287+
ActiveSupport::Notifications.instrument(name, owner: owner, reflection: reflection)
288288
end
289289
end
290290
end

activerecord/lib/active_record/log_subscriber.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ def self.reset_runtime
2222
def strict_loading_violation(event)
2323
debug do
2424
owner = event.payload[:owner]
25-
association = event.payload[:association]
25+
association = event.payload[:reflection].klass
26+
name = event.payload[:reflection].name
2627

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)
2829
end
2930
end
3031

activerecord/test/cases/strict_loading_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def test_strict_loading_violation_can_log_instead_of_raise
407407
developer.strict_loading!
408408
assert_predicate developer, :strict_loading?
409409

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
411411
developer.audit_logs.to_a
412412
end
413413
ensure

0 commit comments

Comments
 (0)