Skip to content

Commit a5e12d6

Browse files
author
Benjamin Alexander
committed
Fix really_destroy used against a has_one relationship that is soft deleted
1 parent fe46c57 commit a5e12d6

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Diff for: lib/paranoia.rb

+3
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ def really_destroy!(update_destroy_attributes: true)
182182
association_data = self.send(name)
183183
# has_one association can return nil
184184
# .paranoid? will work for both instances and classes
185+
if association_data.nil? && reflection.has_one? && reflection.klass.paranoid?
186+
association_data = reflection.klass.only_deleted.find_by(reflection.foreign_key => self.id)
187+
end
185188
next unless association_data && association_data.paranoid?
186189
if reflection.collection?
187190
next association_data.with_deleted.find_each { |record|

Diff for: test/paranoia_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,16 @@ def test_update_has_many_through_relation_delete_associations
14181418
assert_equal 2, employer.jobs.with_deleted.count
14191419
end
14201420

1421+
def test_really_destroy_against_soft_deleted_object_with_has_one_association
1422+
model = ParanoidModelWithHasOne.create(paranoid_model_with_belong: ParanoidModelWithBelong.create)
1423+
assert_equal 1, ParanoidModelWithBelong.with_deleted.count
1424+
model.destroy
1425+
assert_equal 1, ParanoidModelWithBelong.with_deleted.count
1426+
model.reload
1427+
model.really_destroy!
1428+
assert_equal 0, ParanoidModelWithBelong.with_deleted.count # I think this should fail.
1429+
end
1430+
14211431
private
14221432
def get_featureful_model
14231433
FeaturefulModel.new(:name => "not empty")

0 commit comments

Comments
 (0)