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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A default_scope silently filters every query, so always provide an escape hatch like with_deleted to reach the hidden rows.
- 2Overriding destroy to set a timestamp keeps records recoverable while preserving callback behavior.
- 3update_column and delete_all skip validations and callbacks, making them fast but blunt tools for bulk soft-delete operations.
Related explainers
ruby
require "csv" class SalesReport def initialize(path)
Aggregating CSV sales data in Ruby
data-aggregation
memoization
group_by
Intermediate
6 steps
ruby
module DurationFormatter UNITS = [ ['week', 604_800], ['day', 86_400],
Turning seconds into human-readable durations in Ruby
greedy-decomposition
modular-arithmetic
formatting
Intermediate
7 steps
ruby
class Comment < ApplicationRecord belongs_to :post belongs_to :author, class_name: "User"
Live-updating comments with Turbo in Rails
turbo-streams
callbacks
associations
Intermediate
8 steps
ruby
require 'json' require 'set' class SensitiveScrubber
Recursively scrubbing secrets from JSON
recursion
data-masking
pattern-matching
Intermediate
7 steps
ruby
class ReportBatcher BATCH_SIZE = 500 def initialize(account)
Batching monthly email summaries in Rails
batching
service-object
background-jobs
Intermediate
7 steps
ruby
class Comment < ApplicationRecord belongs_to :commentable, polymorphic: true, counter_cache: true belongs_to :author, class_name: "User"
How polymorphic comments work in Rails
polymorphic-association
concerns
counter-cache
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-soft-delete-concern-works-in-rails-explained-ruby-05ff/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.