Skip to content

Comments

Fix duplicate DELETE statements for STI models sharing a table#22

Merged
ngan merged 1 commit intomainfrom
rdg.fix-sti-duplicate-deletes
Feb 24, 2026
Merged

Fix duplicate DELETE statements for STI models sharing a table#22
ngan merged 1 commit intomainfrom
rdg.fix-sti-duplicate-deletes

Conversation

@robbiegill
Copy link
Contributor

Summary

  • When multiple STI models share the same table (e.g. Car and Truck both using vehicles), Cache#statements_by_connection was generating a DELETE FROM for each model. The second DELETE would wipe records inserted by a sibling model earlier in the batch.
  • Track deleted tables per connection with a Set so the DELETE is only emitted once per table.
  • Use unscoped in generate_statements so default scopes (including STI type conditions) don't filter out records during cache generation.

Test plan

  • Added STI models (Vehicle, Car, Truck) to the dummy app
  • Added integration specs (both RSpec and Minitest) verifying all STI subclass records survive a cache save + load round-trip
  • Full test suite passes (85 examples, 0 failures)

🤖 Generated with Claude Code

When multiple STI models share the same table (e.g. Car and Truck both
using vehicles), statements_by_connection was generating a DELETE FROM
for each model. The second DELETE would wipe records inserted by a
sibling model earlier in the batch.

Track deleted tables per connection with a Set so the DELETE is only
emitted once per table. Also use `unscoped` in generate_statements so
default scopes (including STI type conditions) don't filter out records
during cache generation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ngan ngan merged commit df2aa23 into main Feb 24, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants