From 4bd7a1107353d5db2ef8b4815919e83cce1dce45 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 19 Feb 2019 21:37:45 -0600 Subject: [PATCH] Add Delete Paranoid Records as a rails til --- README.md | 3 ++- rails/delete-paranoid-records.md | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 rails/delete-paranoid-records.md diff --git a/README.md b/README.md index 2035d5e..7ccc628 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_768 TILs and counting..._ +_769 TILs and counting..._ --- @@ -470,6 +470,7 @@ _768 TILs and counting..._ - [Convert A Symbol To A Constant](rails/convert-a-symbol-to-a-constant.md) - [Creating Records of Has_One Associations](rails/creating-records-of-has-one-associations.md) - [Custom Validation Message](rails/custom-validation-message.md) +- [Delete Paranoid Records](rails/delete-paranoid-records.md) - [Demodulize A Class Name](rails/demodulize-a-class-name.md) - [Generating And Executing SQL](rails/generating-and-executing-sql.md) - [Hash Slicing](rails/hash-slicing.md) diff --git a/rails/delete-paranoid-records.md b/rails/delete-paranoid-records.md new file mode 100644 index 0000000..da8f92a --- /dev/null +++ b/rails/delete-paranoid-records.md @@ -0,0 +1,21 @@ +# Delete Paranoid Records + +The [ActsAsParanoid gem](https://github.com/ActsAsParanoid/acts_as_paranoid) +provides soft delete functionality to `ActiveRecord` objects in Rails. You +can enhance a model with its functionality like so: + +```ruby +class User < ActiveRecord::Base + acts_as_paranoid +end +``` + +This gem hijacks `ActiveRecord`'s standard `destroy` and `destroy!` +functionality. If you call either of these methods, instead of the record +being deleted from the database, it's `deleted_at` column is updated from +`nil` to the current timestamp. Resulting in a _soft deleted_ record. + +If you call `destroy` or `destroy!` a second time (i.e. on a record that has +already been soft deleted), it will be actually deleted from the database. +Alternatively, you can call `destroy_fully!` from the beginning to skip the +soft delete.