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
ruby
module Api
  module V1
    class PostsController < BaseController
      def index
        # Eager load author and recent comments with their authors
        posts = Post.published

N+1 prevention with includes and preload

rails activerecord performance
by Alex Kumar 1 tab
ruby
class User < ApplicationRecord
  has_many :posts, foreign_key: :author_id

  validates :email, presence: true,
                    uniqueness: { case_sensitive: false },
                    format: { with: URI::MailTo::EMAIL_REGEXP }

Model validations for data integrity

rails activerecord validation
by Alex Kumar 1 tab
yaml
production:
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS", 5) %>
  timeout: 5000
  checkout_timeout: 5

Database connection pooling configuration

rails database performance
by Alex Kumar 2 tabs
ruby
module ApiRequestLogger
  extend ActiveSupport::Concern

  included do
    around_action :log_api_request
  end

API request logging for debugging and analytics

rails logging observability
by Alex Kumar 1 tab