Feedback on regression fix for autosave associations


I’m after some feedback on my PR It fixes a regression introduced in Rails 6.

Prior to Rails 6, validating a parent record that had a singular autosaving/validating association would always result in the child’s validations firing. A change to resolve another issue now means that the child’s validations only fire if the child record is dirty. This change made the behaviour for singular associations consistent with that of collection associations, and makes sense if we assume an already-persisted non-dirty record will remain valid. This is unhelpful however in scenarios where custom validation contexts are being used.

Take this example from the PR where a validation with a custom :bank_loan context exists on the child association:

firm_with_low_credit = Firm.create!(name: “Generic Startup", account: 50))
assert firm_with_low_credit.valid?
assert_not firm_with_low_credit.valid?(:bank_loan)

This now fails because the validations are not fired on the non-dirty account association.

My feeling is that whilst it generally makes sense to not fire validations on non-dirty associations, if a custom validation context is used then all bets should be considered off as the context is likely different to when the record was originally saved, and so the validity of the associations should be re-considered.

The PR:

For further background, skipping validations on non-dirty associations was originally introduced as a way prevent stack level too deep issue -