Skip to content

Commit

Permalink
Add the find_target? check to the ActiveRecord::Associations::Associa…
Browse files Browse the repository at this point in the history
…tion#violates_strict_loading?
  • Loading branch information
shes50103 committed Apr 22, 2024
1 parent 07bd754 commit f0c9f34
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions activerecord/lib/active_record/associations/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit f0c9f34

Please sign in to comment.