ruby

ruby
# Basic Ractor usage
ractor = Ractor.new do
  # This runs in parallel, isolated from main thread
  result = heavy_computation
  result
end

Concurrent Ruby with Ractors and Async

ruby concurrency parallelism
by Sarah Mitchell 2 tabs
ruby
class PostsController < ApplicationController
  def create
    @post = current_member.posts.build(post_params)

    if @post.save
      redirect_to @post, status: :see_other

Inline form validation feedback via Turbo Streams

rails hotwire turbo
by Henry Kim 2 tabs
ruby
class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
  belongs_to :author, class_name: "User"

  scope :visible, -> { where(deleted_at: nil).order(:created_at) }

Turbo Streams: partial replace with morphing (less jitter)

rails turbo hotwire
by codesnips 4 tabs
erb
<%= turbo_stream_from [Current.account, :notifications] %>
<div id="notifications"></div>

Per-account stream scoping to prevent “cross-tenant” updates

rails hotwire turbo
by Henry Kim 2 tabs
ruby
class Current < ActiveSupport::CurrentAttributes
  attribute :user, :tenant, :request_id, :ip_address

  def user_display
    user&.email || "system"
  end

CurrentAttributes for Request-Scoped Context

rails activesupport currentattributes
by codesnips 4 tabs
ruby
module Paginatable
  extend ActiveSupport::Concern

  included do
    before_action :set_pagination_params, only: [:index]
  end

Rails concerns for shared controller behavior

rails concerns controllers
by Maya Patel 2 tabs
ruby
class CreatePostService
  def initialize(author:, params:)
    @author = author
    @params = params
  end

Service objects for complex business logic

rails architecture patterns
by Alex Kumar 1 tab
ruby
# Gemfile
gem 'graphql'

# Installation
# rails generate graphql:install

GraphQL APIs with graphql-ruby gem

ruby rails graphql
by Sarah Mitchell 3 tabs
ruby
user = User.find_by(email: params[:email].to_s.downcase.strip)

if user
  raw_token = SecureRandom.urlsafe_base64(32)
  user.password_resets.create!(token_digest: Digest::SHA256.hexdigest(raw_token), expires_at: 30.minutes.from_now)
  PasswordResetMailer.with(user: user, token: raw_token).deliver_later

Password reset flow that avoids user enumeration and token leaks

password-reset account-enumeration authentication
by Kai Nakamura 1 tab
ruby
allowed_types = ['image/png', 'image/jpeg', 'application/pdf']
uploaded = params.require(:document)

raise ActionController::BadRequest, 'file too large' if uploaded.size > 10.megabytes
raise ActionController::BadRequest, 'type not allowed' unless allowed_types.include?(uploaded.content_type)

Hardening file uploads with MIME checks and storage isolation

file-uploads validation malware
by Kai Nakamura 1 tab
ruby
class AddStatusToPosts < ActiveRecord::Migration[6.1]
  # Use change for automatic rollback
  def change
    # Add column without default to avoid table lock
    add_column :posts, :status, :string

Rails database migrations best practices

rails migrations database
by Maya Patel 3 tabs
yaml
production:
  primary:
    adapter: postgresql
    url: <%= ENV['DATABASE_URL'] %>
    pool: <%= ENV.fetch("RAILS_MAX_THREADS", 5) %>
  primary_replica:

Database read replicas for scaling reads

rails postgresql scaling
by Alex Kumar 3 tabs