ruby
36 lines · 8 steps
Live-updating comments with Turbo in Rails
A Comment model pushes its own creates, updates, and deletes to subscribers over Turbo Streams.
Explained by
highlit
1class Comment < ApplicationRecord
2 belongs_to :post
3 belongs_to :author, class_name: "User"
4
5 validates :body, presence: true, length: { maximum: 2_000 }
6
7 scope :recent, -> { order(created_at: :desc) }
8
9 after_create_commit :broadcast_creation
10 after_update_commit :broadcast_replacement
11 after_destroy_commit :broadcast_removal
12
13 private
14
15 def broadcast_creation
16 broadcast_prepend_later_to(
17 [post, :comments],
18 target: dom_id(post, :comments),
19 partial: "comments/comment",
20 locals: { comment: self }
21 )
22 broadcast_update_later_to(
23 [post, :comments],
24 target: dom_id(post, :comments_count),
25 html: post.comments.count
26 )
27 end
28
29 def broadcast_replacement
30 broadcast_replace_later_to([post, :comments], target: self)
31 end
32
33 def broadcast_removal
34 broadcast_remove_to([post, :comments], target: self)
35 end
36end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Commit-time callbacks fire only after the transaction succeeds, so broadcasts never reference uncommitted data.
- 2Streaming to a shared `[post, :comments]` stream lets every subscriber receive the same DOM updates.
- 3Targeting elements by `dom_id` keeps server-rendered partials and client DOM in sync without custom JavaScript.
Related explainers
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
ruby
class SessionsController < ApplicationController MAX_ATTEMPTS = 5 THROTTLE_WINDOW = 15.minutes
Throttling failed logins in Rails
rate-limiting
authentication
caching
Intermediate
7 steps
ruby
class ProductsController < ApplicationController def index @products = Product .includes(:category, :brand)
Building a safe filterable product index in Rails
query objects
scopes
strong parameters
Intermediate
8 steps
ruby
module LogParser class AccessLogStreamer SEVERITY_PATTERN = /\b(ERROR|WARN|FATAL)\b/
Streaming log lines with a Ruby enumerator
enumerators
lazy-iteration
regex-matching
Intermediate
6 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/live-updating-comments-with-turbo-in-rails-explained-ruby-5500/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.