Skip to content

Add option to defer persisting additions/removals on collection associations. Add marked_for_deletion. #55041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

WelpThatWorked
Copy link

Motivation / Background

Presently, any additions or removals of existing records to a collection association are persisted to the database immediately, rather than waiting for save to be called manually. This can be unexpected or undesirable in cases such as generating a preview or looking back at what changes just occurred. While there exist some workarounds, they seem a bit cumbersome, and there have occasionally been requests for it to not be persisted immediately, so I thought having a toggle on that behavior might be more convenient.

Detail

  • Add the :defer option for collection associations with autosave enabled. Setting this to true will defer the persisting of additions/removals until the parent (or child, in the case of has_many) is saved.
    • Applies to methods using concat() or replace(). Using collection.destroy(record) or collection.delete(record) still persists immediately, since mark_for_deletion/destruction is simultaneously available.
    • Removals are flagged for deletion, while additions are autosaved due to changes in the record (has_many) or changes in the through record (habtm, has_many :through)
    • Reloading a habtm or has_many :through association now resets the internal through association to keep the records listed on both targets in sync.
  • Add marked_for_deletion to Autosave Associations, a mirror of marked_for_destruction that calls delete instead of destroy.
    • Add marked_for_removal? which checks if either flag is set.
    • If both flags are set, destruction takes precedence.
    • I'm concerned the wording of some of the documentation in autosave_association.rb might be a bit messy where the two overlap, but I'm unsure.
  • Add change-tracking methods <association>_added_records and <association>_previously_added_records
    • I'm not sure this is worthwhile, as it might be more flexibly accomplished by the use of added/removed callback, and implementing similar methods for removals seems it would require keeping a copy of deleted/destroyed records in memory regardless of if the function would be used.
    • Not sure about the name.

Additional information

Other previous related discussion:
#17368
#42097
#46685
Rails Forum

…or_deletion`.

Add option to defer persisting added/removed records on autosave collection associations.
Add `marked_for_deletion` to Autosave Associations, a mirror of `marked_for_destruction` that calls `delete` instead of `destroy`
@WelpThatWorked WelpThatWorked force-pushed the defer-autosave-collection branch from f4ab493 to b85b941 Compare May 25, 2025 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant