Skip to content

Commit

Permalink
Fix really_destroy used against a has_one relationship that is soft d…
Browse files Browse the repository at this point in the history
…eleted
  • Loading branch information
Benjamin Alexander committed Feb 6, 2025
1 parent fe46c57 commit a5e12d6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
10 changes: 10 additions & 0 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit a5e12d6

Please sign in to comment.