From a5e12d6ada693c3a04c0020c5b852bb0924dfdaf Mon Sep 17 00:00:00 2001 From: Benjamin Alexander Date: Thu, 6 Feb 2025 08:51:21 +0000 Subject: [PATCH] Fix really_destroy used against a has_one relationship that is soft deleted --- lib/paranoia.rb | 3 +++ test/paranoia_test.rb | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/paranoia.rb b/lib/paranoia.rb index 93bf9730..e44e82ed 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -182,6 +182,9 @@ def really_destroy!(update_destroy_attributes: true) association_data = self.send(name) # has_one association can return nil # .paranoid? will work for both instances and classes + if association_data.nil? && reflection.has_one? && reflection.klass.paranoid? + association_data = reflection.klass.only_deleted.find_by(reflection.foreign_key => self.id) + end next unless association_data && association_data.paranoid? if reflection.collection? next association_data.with_deleted.find_each { |record| diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index e11dc416..aa0f4dd1 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -1418,6 +1418,16 @@ def test_update_has_many_through_relation_delete_associations assert_equal 2, employer.jobs.with_deleted.count end + def test_really_destroy_against_soft_deleted_object_with_has_one_association + model = ParanoidModelWithHasOne.create(paranoid_model_with_belong: ParanoidModelWithBelong.create) + assert_equal 1, ParanoidModelWithBelong.with_deleted.count + model.destroy + assert_equal 1, ParanoidModelWithBelong.with_deleted.count + model.reload + model.really_destroy! + assert_equal 0, ParanoidModelWithBelong.with_deleted.count # I think this should fail. + end + private def get_featureful_model FeaturefulModel.new(:name => "not empty")