ruby
class CommentsController < ApplicationController
  before_action :set_post

  def new
    @comment = @post.comments.build
  end

Turbo Stream form errors: replace only the form frame

rails turbo hotwire
by codesnips 4 tabs
ruby
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :author, class_name: "User"

  validates :body, presence: true

Model broadcasts: prepend on create, replace on update

rails hotwire turbo-streams
by codesnips 4 tabs
erb
<%= turbo_frame_tag dom_id(contact) do %>
  <tr class="contact-row">
    <td><%= contact.name %></td>
    <td><%= contact.email %></td>
    <td><%= contact.role.titleize %></td>
    <td class="actions">

Turbo Frames: Inline edit that swaps form <-> row

rails turbo hotwire
by codesnips 3 tabs
ruby
class CommentsController < ApplicationController
  before_action :set_post

  def create
    @comment = @post.comments.build(comment_params)

Turbo Streams: Create with prepend + HTML fallback

rails turbo hotwire
by codesnips 4 tabs
ruby
class ApplicationCommand
  Result = Struct.new(:value, :errors) do
    def success?
      errors.nil? || errors.empty?
    end

Keep Controllers Thin: Use Command Objects

rails architecture command-object
by codesnips 3 tabs
ruby
class ReindexCheckpoint < ApplicationRecord
  enum status: { idle: 0, running: 1, done: 2, failed: 3 }

  validates :index_name, presence: true, uniqueness: true

  def self.for(index_name)

Safer Background Reindex: slice batches + checkpoints

rails reliability elasticsearch
by codesnips 4 tabs
ruby
class Category < ApplicationRecord
  has_many :products, dependent: :restrict_with_error
  has_many :subcategories,
           class_name: "Category",
           foreign_key: :parent_id,
           dependent: :restrict_with_error

Safer Deletion with dependent: :restrict_with_error

rails activerecord data-integrity
by codesnips 4 tabs
ruby
class CreateEmailDeliveries < ActiveRecord::Migration[7.1]
  def change
    create_table :email_deliveries do |t|
      t.string :dedupe_key, null: false
      t.string :mailer, null: false
      t.string :action, null: false

Transactional Email “Send Once” with Delivered Marker

rails reliability activerecord
by codesnips 4 tabs
ruby
class Tag < ApplicationRecord
  has_many :taggings, dependent: :destroy

  scope :top, ->(limit = 20) {
    joins(:taggings)
      .group(Arel.sql("tags.id"))

Memory-Safe “top tags” aggregation with pluck + group

rails activerecord performance
by codesnips 3 tabs
ruby
module WriteAmplificationGuard
  extend ActiveSupport::Concern

  def update_if_changed(attrs)
    changed = attrs.each_with_object({}) do |(key, value), acc|
      cast = self.class.type_for_attribute(key.to_s).cast(value)

“Write Amplification” Guard: Only Update Changed Columns

rails activerecord performance
by codesnips 3 tabs
ruby
module DefensiveDeserialization
  extend ActiveSupport::Concern

  MissingRecord = Struct.new(:gid) do
    def missing?
      true

Defensive Deserialization for ActiveJob

rails activejob reliability
by codesnips 3 tabs
ruby
module RetryableTransaction
  module_function

  RETRIABLE = [ActiveRecord::Deadlocked, ActiveRecord::LockWaitTimeout].freeze

  def run(max_retries: 3, base_delay: 0.05, &block)

Deadlock-Aware Retry Wrapper

rails postgres reliability
by codesnips 3 tabs