Skip to content

Commit 832554f

Browse files
authored
Merge pull request rails#55115 from rails/fxn/cache-reflection-validation
Memoize successful reflection cache validation
2 parents c19760f + 43925c3 commit 832554f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

activerecord/lib/active_record/reflection.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ def compute_class(name)
520520

521521
def initialize(name, scope, options, active_record)
522522
super
523+
524+
@validated = false
523525
@type = -(options[:foreign_type]&.to_s || "#{options[:as]}_type") if options[:as]
524526
@foreign_type = -(options[:foreign_type]&.to_s || "#{name}_type") if options[:polymorphic]
525527
@join_table = nil
@@ -620,6 +622,8 @@ def join_foreign_key
620622
end
621623

622624
def check_validity!
625+
return if @validated
626+
623627
check_validity_of_inverse!
624628

625629
if !polymorphic? && (klass.composite_primary_key? || active_record.composite_primary_key?)
@@ -629,6 +633,8 @@ def check_validity!
629633
raise CompositePrimaryKeyMismatchError.new(self)
630634
end
631635
end
636+
637+
@validated = true
632638
end
633639

634640
def check_eager_loadable!
@@ -979,6 +985,8 @@ class ThroughReflection < AbstractReflection # :nodoc:
979985

980986
def initialize(delegate_reflection)
981987
super()
988+
989+
@validated = false
982990
@delegate_reflection = delegate_reflection
983991
@klass = delegate_reflection.options[:anonymous_class]
984992
@source_reflection_name = delegate_reflection.options[:source]
@@ -1142,6 +1150,8 @@ def through_options
11421150
end
11431151

11441152
def check_validity!
1153+
return if @validated
1154+
11451155
if through_reflection.nil?
11461156
raise HasManyThroughAssociationNotFoundError.new(active_record, self)
11471157
end
@@ -1179,6 +1189,8 @@ def check_validity!
11791189
end
11801190

11811191
check_validity_of_inverse!
1192+
1193+
@validated = true
11821194
end
11831195

11841196
def constraints

0 commit comments

Comments
 (0)