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
yaml
amazon:
  service: S3
  access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
  secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
  region: us-east-1
  bucket: <%= Rails.application.credentials.dig(:aws, :bucket) %>

ActiveStorage for file uploads

rails activestorage file-uploads
by Alex Kumar 3 tabs
ruby
module Api
  module V1
    class UsersController < BaseController
      def show
        user = User.includes(:profile).find(params[:id])

ETags for conditional requests and caching

rails caching http-caching
by Alex Kumar 1 tab
yaml
cleanup_expired_sessions:
  cron: '0 2 * * *'  # Daily at 2 AM
  class: CleanupExpiredSessionsWorker
  queue: low
  description: Remove expired sessions from Redis

Background job scheduling with sidekiq-scheduler

rails sidekiq background-jobs
by Alex Kumar 2 tabs
ruby
class PasswordResetsController < ApplicationController
  before_action :find_user_by_token, only: [:edit, :update]

  def create
    user = User.find_by(email: params[:email]&.downcase)

Secure password reset flow with signed tokens

rails security authentication
by Alex Kumar 1 tab
ruby
Apartment.configure do |config|
  config.excluded_models = %w[Tenant User]
  config.tenant_names = -> { Tenant.pluck(:schema_name) }
  config.use_schemas = true
end

Multi-tenancy with apartment gem

rails multi-tenancy postgresql
by Alex Kumar 3 tabs
ruby
module MyApp
  class Application < Rails::Application
    config.load_defaults 6.1

    # Enable response compression
    config.middleware.use Rack::Deflater

API response compression with Rack::Deflater

rails performance api
by Alex Kumar 1 tab
ruby
module Idempotency
  extend ActiveSupport::Concern

  included do
    before_action :check_idempotency_key, only: [:create, :update]
    after_action :store_idempotent_response, only: [:create, :update]

Request deduplication with idempotency keys

rails api reliability
by Alex Kumar 1 tab
ruby
class AddMetadataToUsers < ActiveRecord::Migration[6.1]
  def change
    add_column :users, :metadata, :jsonb, default: {}, null: false
    add_index :users, :metadata, using: :gin
  end
end

JSON column for flexible schema extensions

rails postgresql database
by Alex Kumar 3 tabs
ruby
require 'activerecord-import'

class BulkImportPostsService
  BATCH_SIZE = 1000

  def initialize(csv_file_path)

Bulk operations with ActiveRecord import

rails performance activerecord
by Alex Kumar 1 tab
ruby
class CreateAuditLogs < ActiveRecord::Migration[6.1]
  def change
    create_table :audit_logs do |t|
      t.references :user, null: true, foreign_key: true
      t.string :action, null: false
      t.string :resource_type

Audit logging for sensitive operations

rails security audit-logging
by Alex Kumar 3 tabs
ruby
class RateLimiter
  def initialize(key:, limit:, window:)
    @key = "rate_limit:#{key}"
    @limit = limit
    @window = window
  end

API throttling with custom Redis-based limiter

rails redis rate-limiting
by Alex Kumar 2 tabs