ruby 34 lines · 8 steps

How a soft-delete concern works in Rails

A concern that hides deleted records by default and turns destruction into a timestamp flag instead of a real delete.

Explained by highlit
1module SoftDeletable
2 extend ActiveSupport::Concern
3 
4 included do
5 default_scope { where(deleted_at: nil) }
6 
7 scope :with_deleted, -> { unscope(where: :deleted_at) }
8 scope :only_deleted, -> { with_deleted.where.not(deleted_at: nil) }
9 end
10 
11 def destroy
12 run_callbacks(:destroy) do
13 update_column(:deleted_at, Time.current)
14 end
15 end
16 
17 def restore
18 update_column(:deleted_at, nil)
19 end
20 
21 def destroy_fully!
22 self.class.with_deleted.where(id: id).delete_all
23 end
24 
25 def deleted?
26 deleted_at.present?
27 end
28 
29 class_methods do
30 def restore_all
31 only_deleted.update_all(deleted_at: nil)
32 end
33 end
34end
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A default_scope silently filters every query, so always provide an escape hatch like with_deleted to reach the hidden rows.
  2. 2Overriding destroy to set a timestamp keeps records recoverable while preserving callback behavior.
  3. 3update_column and delete_all skip validations and callbacks, making them fast but blunt tools for bulk soft-delete operations.

Related explainers

Share this explainer

Here's the card — post it anywhere.

How a soft-delete concern works in Rails — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code