Skip to content

Commit

Permalink
Skip new through targets when autosaving collection associations
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshenny committed Jan 10, 2025
1 parent 035485e commit 8d26b94
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion activerecord/lib/active_record/autosave_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,14 @@ def init_internals
# unless the parent is/was a new record itself.
def associated_records_to_validate_or_save(association, new_record, autosave)
if new_record || custom_validation_context?
association && association.target
target = association && association.target
if target && association.reflection.through_reflection
# We expect that new through records would eventually be
# autosaved by their direct parent, so we can ignore them here.
target.find_all(&:changed_for_autosave?)
else
target
end
elsif autosave
association.target.find_all(&:changed_for_autosave?)
else
Expand Down

0 comments on commit 8d26b94

Please sign in to comment.