Skip to content

Commit 32bac31

Browse files
committed
Spell checking if record and association_name are provided for AssociationNotFoundError
1 parent 2f078aa commit 32bac31

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

activerecord/lib/active_record/associations.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module ActiveRecord
44
class AssociationNotFoundError < ConfigurationError #:nodoc:
55
attr_reader :record, :association_name
6+
67
def initialize(record = nil, association_name = nil)
78
@record = record
89
@association_name = association_name
@@ -17,16 +18,21 @@ def initialize(record = nil, association_name = nil)
1718
include DidYouMean::Correctable
1819

1920
def corrections
20-
@corrections ||= begin
21-
maybe_these = record&.class&.reflections&.keys
22-
DidYouMean::SpellChecker.new(dictionary: maybe_these).correct(association_name.to_s)
21+
if record && association_name
22+
@corrections ||= begin
23+
maybe_these = record.class.reflections.keys
24+
DidYouMean::SpellChecker.new(dictionary: maybe_these).correct(association_name)
25+
end
26+
else
27+
[]
2328
end
2429
end
2530
end
2631
end
2732

2833
class InverseOfAssociationNotFoundError < ActiveRecordError #:nodoc:
2934
attr_reader :reflection, :associated_class
35+
3036
def initialize(reflection = nil, associated_class = nil)
3137
if reflection
3238
@reflection = reflection

0 commit comments

Comments
 (0)