Skip to content

Prevent autosave association with has_one defined on child class

Created by: dfritsch

Motivation / Background

Adjusts the logic in inverse_polymorphic_association_changed? to determine if a has_one association's polymorphic association should autosave the associated record for a type change.

Previously this would see the type as changed every time if the has_one is defined on a child class, since the polymorphic relationship saves the parent class as the *_type value. The new check resolves the correct class name to avoid these extra saves.

Fixes #51280

Detail

Also confirmed that the new test fails without the code change:

Failure:
TestAutosaveAssociationOnAHasOneAssociation#test_should_not_saved_for_unchanged_sti_type_on_polymorphic_association [test/cases/autosave_association_test.rb:1652]:
Expected: 2
  Actual: 3


bin/rails test test/cases/autosave_association_test.rb:1620

Additional information

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Merge request reports