ruby

ruby
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
  default from: 'noreply@example.com'

  def welcome_email(user)
    @user = user

ActionMailer advanced patterns for transactional emails

ruby rails action-mailer
by Sarah Mitchell 2 tabs
ruby
# BAD: N+1 query problem
@users = User.all
@users.each do |user|
  puts user.posts.count  # Fires query for each user!
end

ActiveRecord query optimization and N+1 prevention

ruby rails activerecord
by Sarah Mitchell 3 tabs
ruby
json.array! @posts do |post|
  json.cache! ['v1', post], expires_in: 1.hour do
    json.id post.id
    json.title post.title
    json.excerpt post.excerpt
    json.published_at post.published_at

Fragment caching for expensive JSON serialization

rails caching performance
by Alex Kumar 1 tab
ruby
RegistrationSchema = Dry::Schema.Params do
  required(:email).filled(:string, format?: URI::MailTo::EMAIL_REGEXP)
  required(:password).filled(:string, min_size?: 12)
  optional(:marketing_opt_in).filled(:bool)
  optional(:country).filled(:string, included_in?: %w[US CA GB AU])
end

Input validation with allowlists and explicit schemas

input-validation schemas secure-coding
by Kai Nakamura 1 tab
ruby
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_member

    def connect
      self.current_member = env['warden']&.user || reject_unauthorized_connection

Presence indicator with ActionCable + Turbo Streams

rails hotwire turbo
by Henry Kim 3 tabs
ruby
class AddEnabledFlagIndexToAccounts < ActiveRecord::Migration[6.1]
  def change
    execute <<~SQL
      CREATE INDEX index_accounts_on_beta_enabled
      ON accounts ((settings->>'beta'))
      WHERE (settings->>'beta') = 'true';

Postgres JSONB Partial Index for Feature Flags

rails postgres jsonb
by Sarah Chen 1 tab
ruby
class Member < ApplicationRecord
  before_validation :normalize_email

  validates :email, presence: true, uniqueness: { case_sensitive: false }
  validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }

Soft Validation: Normalize + Validate Email

rails activerecord validation
by Sarah Chen 1 tab
ruby
class CodeBlock < ApplicationRecord
  belongs_to :snip, touch: true
end

Granular Cache Invalidation with touch: true

rails caching activerecord
by Sarah Chen 2 tabs
ruby
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :author, class_name: 'Member'

  after_create_commit -> { broadcast_prepend_later_to(post, target: dom_id(post, :comments)) }
end

Live comments with model broadcasts + turbo_stream_from

rails hotwire turbo
by Henry Kim 3 tabs
ruby
# Installation
# rails active_storage:install
# rails db:migrate

# config/storage.yml
local:

ActiveStorage for file uploads and attachments

ruby rails active-storage
by Sarah Mitchell 2 tabs
erb
<%# users can only subscribe to streams signed for them %>
<%= turbo_stream_from current_member.signed_stream_name %>

Turbo Streams + authorization: signed per-user stream name

rails turbo hotwire
by Sarah Chen 2 tabs
erb
<%= form_with url: issues_path, method: :get, data: { turbo_frame: 'issue_results' } do |f| %>
  <%= f.select :status, options_for_select(%w[open closed], params[:status]), include_blank: 'Any' %>
  <%= f.submit 'Apply', class: 'ml-2 rounded bg-gray-100 px-3 py-1' %>
<% end %>

<%= turbo_frame_tag 'issue_results' do %>

Frame navigation that targets a specific frame via form_with

rails hotwire turbo
by Henry Kim 2 tabs