[ActiveRecord] Fix this (data loss?) bug in association assignment (has PR)

A while back I discovered a bug in ActiveRecord::Associations::CollectionProxy#replace and fixed it. See links to issue and PR below. I think it can be called a data-loss issue, though it’s unlikely to hit by surprise.

I created a reproducing test case (link also below), tried to follow contribution guidelines, and wrote a patch.

The problem is with assignment to has_many-through associations, when also some occurrences are duplicate and preexist.

The cause is a conceptually wrong use of Array#- which has set semantics, breaking down in the duplicate use case.

The rails-bot promised I should hear feedback shortly, but that didn’t happen, yet. I do understand the constraints of Open Source and how they apply here.

I just edited, simplified, shortened and generally (hopefully) made the bug report and PR more compelling.

Also, rebased the PR just a few minutes ago.

I’d be very happy about some feedback from rails-core - on the change, and how it can be made acceptable for inclusion.

Have a nice Sunday!

Florian

Issue: Duplicates lost on `collection=objects` and `collection_singular_ids=ids` for `has_many :through` association · Issue #33942 · rails/rails · GitHub

Repro Test Case: active_record_gem.rb · GitHub

PR: Fix handling of duplicates for `replace` on has_many-through by febeling · Pull Request #33954 · rails/rails · GitHub