Hiya,

I’m after some feedback on my PR <https://github.com/rails/rails/pull/37295>  
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: 
Account.new(credit_limit: 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: https://github.com/rails/rails/pull/37295

For further background, skipping validations on non-dirty associations was 
originally introduced as a way prevent stack level too deep issue - 
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2578-accepts_nested_attributes_for-causes-stack-level-too-deep

Tekin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-core/CBBCA8EF-9959-4CF3-AC3F-9EF807772BB3%40tekin.co.uk.

Reply via email to