Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix really_destroy used against a has_one relationship that is soft deleted #574

Open
wants to merge 1 commit into
base: core
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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