ruby

ruby
# In rails console
query = Post.joins(:author)
           .where(published_at: 1.week.ago..Time.current)
           .where(users: { status: 'active' })
           .order(created_at: :desc)

Database query explain analysis for optimization

rails postgresql performance
by Alex Kumar 2 tabs
ruby
class PostsController < ApplicationController
  def create
    @post = current_user.posts.build(post_params)

    if @post.save
      flash[:success] = 'Post was successfully created.'

Rails Flash messages for user feedback

rails flash messages
by Maya Patel 2 tabs
ruby
AuditLog.create!(
  actor_id: current_user.id,
  action: 'member.approve',
  target_type: 'Member',
  target_id: member.id,
  ip_address: request.remote_ip,

Structured audit logging for privileged actions

audit-logging siem observability
by Kai Nakamura 1 tab
erb
<nav class="flex gap-2">
  <%= link_to 'Profile', settings_path(tab: 'profile'), data: { turbo_frame: 'tab_content' } %>
  <%= link_to 'Billing', settings_path(tab: 'billing'), data: { turbo_frame: 'tab_content' } %>
</nav>

<%= turbo_frame_tag 'tab_content' do %>

Tabs UI using Turbo Frames (no client router)

rails hotwire turbo
by Henry Kim 2 tabs
ruby
class Message < ApplicationRecord
  belongs_to :room
  broadcasts_to ->(message) { [message.room, :messages] }, inserts_by: :prepend
end

Declarative model broadcasts with broadcasts_to (Rails 7)

rails hotwire turbo
by Henry Kim 2 tabs
ruby
Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :users, only: [:index, :show, :create, :update]
      resources :posts do
        resources :comments, only: [:index, :create]

API versioning with namespace routing

rails api versioning
by Alex Kumar 2 tabs
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["field"]

  connect() {

Stimulus: autofocus the first invalid field after Turbo update

rails stimulus hotwire
by codesnips 3 tabs
ruby
respond_to do |format|
  format.turbo_stream
  format.html { redirect_to projects_path, notice: 'Created.' }
end

Turbo Streams fallback to HTML for older clients

rails hotwire turbo
by Henry Kim 2 tabs
ruby
class WizardsController < ApplicationController
  before_action :load_draft

  def step_1
    render_step(1)
  end

Hotwire-powered multi-step forms

hotwire turbo forms
by Jordan Lee 3 tabs
ruby
module Middleware
  class RequestTimer
    def initialize(app)
      @app = app
    end

Custom middleware for request tracking

rails rack middleware
by Alex Kumar 2 tabs
ruby
class Maintenance::BackfillSnipScoresJob < ApplicationJob
  queue_as :maintenance

  def perform
    ActiveRecord::Base.connection_pool.with_connection do |conn|
      conn.verify!

Keep DB Connections Healthy in Long Jobs

rails activerecord background-jobs
by Sarah Chen 1 tab
erb
<article class="post">
  <h1><%= @post.title %></h1>
  <%= simple_format @post.body %>

  <!-- Lazy load comments when scrolled into view -->
  <%= turbo_frame_tag "post_#{@post.id}_comments",

Lazy-loading Turbo Frames for performance

hotwire turbo performance
by Jordan Lee 3 tabs