Skip to content

Commit

Permalink
Skip void delete calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynetics committed Apr 27, 2023
1 parent 9c912c3 commit e766284
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## Unreleased
### Fixed
- fixed unnecessary void DELETE calls for empty relations
- thanks to [Richard Nienaber](https://github.com/rjnienaber) for the report

## v1.2.1
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion lib/delete_recursively.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def delete_recursively(reflection, _legacy_arg, owner_ids, seen: [], force: fals

if dest_method = destructive_method(reflection, force: force)
record_ids ||= DependentIdFinder.call(owner_ids, reflection, assoc_class)
assoc_class.send(dest_method, record_ids)
assoc_class.send(dest_method, record_ids) unless record_ids.empty?
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/delete_recursively_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
blog.destroy!
end

it 'does not trigger DB calls for empty relations' do
expect(Post).to receive(:delete)
expect(Comment).to receive(:destroy)
blog_with_posts_and_comments.destroy!

expect(Post).not_to receive(:delete)
expect(Comment).not_to receive(:destroy)
Blog.create!.destroy!
end

it 'works on has_one: associations' do
delivery_service = DeliveryService.create!
pizza = delivery_service.pizzas.new
Expand Down

0 comments on commit e766284

Please sign in to comment.