ruby
class NotificationsChannel < ApplicationCable::Channel
  def subscribed
    stream_for current_user
  end

  def unsubscribed

Action Cable for real-time WebSocket communication

rails actioncable websockets
by Alex Kumar 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
module Types
  class PostType < Types::BaseObject
    field :id, ID, null: false
    field :title, String, null: false
    field :body, String, null: false
    field :excerpt, String, null: true

GraphQL API with graphql-ruby gem

rails graphql api
by Alex Kumar 2 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
ruby
class PaymentService
  def initialize
    Stripe.api_key = Rails.application.credentials.stripe[:secret_key]
  end

  def create_payment_intent(amount:, currency: 'usd')

Environment-specific configuration with Rails credentials

rails security configuration
by Alex Kumar 2 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
require 'swagger_helper'

RSpec.describe 'Api::V1::Posts', type: :request do
  path '/api/v1/posts' do
    get 'Retrieves all posts' do
      tags 'Posts'

API documentation with Swagger/OpenAPI

rails api documentation
by Alex Kumar 1 tab
ruby
class TrendingPostsService
  CACHE_KEY = 'trending_posts:v1'.freeze
  CACHE_TTL = 15.minutes

  def self.call(limit: 10)
    Rails.cache.fetch(CACHE_KEY, expires_in: CACHE_TTL) do

Redis caching for expensive computations

rails redis caching
by Alex Kumar 1 tab
ruby
class AddConstraintsToUsers < ActiveRecord::Migration[6.1]
  def change
    # Null constraints
    change_column_null :users, :email, false
    change_column_null :users, :username, false

Database constraints for data integrity

rails postgresql database
by Alex Kumar 1 tab
ruby
class CreateComments < ActiveRecord::Migration[6.1]
  def change
    create_table :comments do |t|
      t.references :commentable, polymorphic: true, null: false
      t.references :author, null: false, foreign_key: { to_table: :users }
      t.text :body, null: false

Polymorphic associations for flexible relationships

rails activerecord database
by Alex Kumar 3 tabs
ruby
class ProcessPaymentWorker
  include Sidekiq::Worker

  sidekiq_options queue: :critical, retry: 10

  sidekiq_retry_in do |count, exception|

Background job retry strategies

rails sidekiq background-jobs
by Alex Kumar 1 tab
ruby
module Webhooks
  class StripeController < ApplicationController
    skip_before_action :verify_authenticity_token

    def create
      payload = request.body.read

Webhook signature verification

rails security webhooks
by Alex Kumar 1 tab