ruby

ruby
module PaginationHeaders
  def set_pagination_headers(scope)
    links = []
    links << page_link('next', scope.next_page) if scope.next_page
    links << page_link('prev', scope.prev_page) if scope.prev_page
    response.headers['Link'] = links.compact.join(', ') if links.any?

API Pagination Headers (Link + Total)

rails api pagination
by Sarah Chen 2 tabs
ruby
cookies.encrypted[:trusted_device] = {
  value: { user_id: current_user.id, fingerprint: device_fingerprint }.to_json,
  expires: 30.days.from_now,
  httponly: true,
  secure: Rails.env.production?,
  same_site: :strict,

Signed and encrypted Rails cookies for tamper resistant state

rails cookies encryption
by Kai Nakamura 1 tab
ruby
# app/channels/chat_channel.rb
class ChatChannel < ApplicationCable::Channel
  def subscribed
    # Subscribe to a specific room
    room = Room.find(params[:room_id])

ActionCable for real-time WebSocket communication

ruby rails actioncable
by Sarah Mitchell 3 tabs
ruby
Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :posts do
        resources :comments, only: [:index, :create]
      end

Rails API versioning strategies

rails api versioning
by Maya Patel 3 tabs
ruby
class Tags::Top
  def call(limit: 20)
    ActsAsTaggableOn::Tagging
      .where(taggable_type: 'Snip')
      .group(:tag_id)
      .order(Arel.sql('COUNT(*) DESC'))

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

rails activerecord performance
by Sarah Chen 1 tab
ruby
class HealthController < ActionController::API
  def show
    checks = {
      db: db_ok?,
      redis: redis_ok?
    }

Health Check Endpoint with Dependency Probes

rails operations reliability
by Sarah Chen 1 tab
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["bar", "percent", "status"]
  static values = {
    current: { type: Number, default: 0 },

Progress indicators for long-running operations

hotwire stimulus actioncable
by Jordan Lee 3 tabs
ruby
class AddDeletedAtToPosts < ActiveRecord::Migration[6.1]
  def change
    add_column :posts, :deleted_at, :datetime
    add_index :posts, :deleted_at
  end
end

Soft deletes with paranoia gem

rails activerecord database
by Alex Kumar 3 tabs
ruby
class FeatureFlags
  def enabled?(name, member: nil)
    key = "ff:#{name}:member:#{member&.id || 'anon'}"

    Rails.cache.fetch(key, expires_in: 30.seconds) do
      flag = FeatureFlag.find_by!(name: name)

Safer Feature Flagging: Cache + DB Fallback

rails caching reliability
by Sarah Chen 1 tab
ruby
# app/validators/order_validator.rb
class OrderValidator < ActiveModel::Validator
  def validate(record)
    validate_order_total(record)
    validate_items_availability(record)
    validate_shipping_address(record)

Custom validators and validation patterns

ruby rails validations
by Sarah Mitchell 3 tabs
ruby
module AdvisoryLock
  module_function

  def with_lock(key)
    lock_id = Zlib.crc32(key.to_s)
    got_lock = ApplicationRecord.connection.select_value("SELECT pg_try_advisory_lock(#{lock_id})")

Idempotent Job with Advisory Lock

rails activejob postgres
by Sarah Chen 2 tabs
ruby
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins_list = if Rails.env.production?
                     ENV['CORS_ALLOWED_ORIGINS']&.split(',') || []
                   else
                     'localhost:3000', 'localhost:5173', /127\.0\.0\.1:\d+/

CORS configuration for cross-origin API requests

rails api security
by Alex Kumar 1 tab