[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!


Issue: https://github.com/rails/rails/issues/33942

Repro Test Case: https://gist.github.com/febeling/06e1da8ad34d5a9c45d41420efbe58bc

PR: https://github.com/rails/rails/pull/33954