rubysherpas / paranoia

acts_as_paranoid for Rails 5, 6 and 7
Other
2.87k stars 524 forks source link

fix: restore has_one with scope #551

Closed zygzagZ closed 4 months ago

zygzagZ commented 5 months ago

When recursively restoring a has_one relation, Paranoia tries to restore any matching deleted record. It ignores the association scope. This can cause both false positives and false negatives when restoring.

This PR imitates this commit from acts_as_paranoid. I am not sure if intentionally, but that commit introduced scope merging for has_one relations there.

when looking for parent.alpha (or beta) to restore, loading association fails, because there are no undeleted records lib/paranoia.rb:204 association_data = send(association.name) = nil the procedure at lib/paranoia.rb:224 ignores association scope and restores the lowest-id record, :gamma effect: beta was never restored and is nil, gamma was restored incorrectly

I am not sure if reflection.scope is the correct way to recover association's scope, acts_as_paranoid does this differently. It works in AR 5.1-7.1 though. I have tested this PR in ruby 3.2.2 in rails 7.1.3 and 6.1.7.6. It also does work in 5.1.7 application, but I failed to resolve gems in ruby 2.5.8/2.6.9 (and run the suite)

zygzagZ commented 5 months ago

I am not sure what happens with has_many relations, they might need patching too.

mathieujobin commented 4 months ago

Thank you very much for this