class Post < ApplicationRecord
has_many :comments, dependent: :destroy
# comments_count is maintained by counter_cache on Comment#belongs_to
scope :stale_comment_counts, lambda {
class CounterFlushJob
include Sidekiq::Job
sidekiq_options queue: :counters, retry: 3
def perform
CounterBuffer.flush_all.each do |field, delta|
class CounterBuffer
DELTA_HASH = "counter:deltas".freeze
READ_RESET = <<~LUA.freeze
local v = redis.call('HGET', KEYS[1], ARGV[1])
if v then redis.call('HDEL', KEYS[1], ARGV[1]) end
<%# Subscribe every viewer of this post to its broadcasts %>
<%= turbo_stream_from @post %>
<%# Update the acting user's button to reflect the toggle %>
<%= turbo_stream.replace dom_id(@post, :like_button) do %>
<%= render "posts/like_button", post: @post %>