diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index c74558e50a8dc..a1519664f3951 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -211,6 +211,8 @@ def create!(attributes = nil, &block) end def violates_strict_loading? + return unless find_target? + return if @skip_strict_loading return unless owner.validation_context.nil? @@ -220,10 +222,6 @@ def violates_strict_loading? owner.strict_loading? && !owner.strict_loading_n_plus_one_only? end - def find_target? - !loaded? && (!owner.new_record? || foreign_key_present?) && klass - end - private # Reader and writer methods call this so that consistent errors are presented # when the association target class does not exist. @@ -291,6 +289,10 @@ def scope_for_create scope.scope_for_create end + def find_target? + !loaded? && (!owner.new_record? || foreign_key_present?) && klass + end + # Returns true if there is a foreign key present on the owner which # references the target. This is used to determine whether we can load # the target if the owner is currently a new record (and therefore diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index f38feb38844b4..e1ac43dfc5a16 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -285,7 +285,7 @@ def calculate(operation, column_name) # # See also #ids. def pluck(*column_names) - if respond_to?(:proxy_association) && proxy_association.violates_strict_loading? && proxy_association.find_target? + if respond_to?(:proxy_association) && proxy_association.violates_strict_loading? Base.strict_loading_violation!(owner: proxy_association.owner.class, reflection: proxy_association.reflection) end