talira

Paranoia

acts_as_paranoid for Rails 5, 6 and 7

open sourceMIT-Modern-Variantself-hostablefree

Paranoia soft-deletes Active Record models: with acts_as_paranoid, destroy sets a deleted_at timestamp instead of removing the row, and a default scope hides deleted records from every query. with_deleted and only_deleted find them, restore brings them back (with their dependent records, optionally within a recovery window), really_destroy! removes them for good, and dependent: :destroy associations are soft-deleted with their parent. It re-implements acts_as_paranoid in much less code. Its README says it isn't recommended for new projects because it overrides Active Record's delete and destroy, points to discard instead, and takes bug fixes and new Rails versions but no new features.

Problems it solves

  • deleted records should be hidden in my Rails app, not removed from the database
  • let users undo a delete and get the record back
  • keep deleted orders for reporting while the app behaves as if they're gone
  • restore a record together with the child records that were deleted with it
  • list only the records that were deleted, or include them in one query
  • permanently purge a hidden record when it really has to go
  • add a deleted_at column and have every query skip those rows automatically
  • stop a deleted record from being attached to another record
  • recover rows someone deleted by mistake without restoring a database backup

Features

  • destroy sets deleted_at; a default scope hides deleted rows
  • with_deleted, without_deleted and only_deleted scopes
  • restore, recursive restore and a recovery window
  • really_destroy! for permanent deletes
  • soft-deletes dependent: :destroy associations
  • custom column name and an option to skip the default scope
  • after_restore_commit callback
  • association_not_soft_destroyed validator

Limitations

  • Not recommended for new projects by its maintainers: it overrides Active Record's delete and destroy, and they point to discard1
  • Doesn't take new features: it gets bug fixes and support for new Rails versions only1

Capabilities

Has an APIplan not known

Alternatives

The same job

These do what Paranoia does.

  1. Discard Soft deletes for ActiveRecord done right

Compare Paranoia with the top 3