Webhook signature verification

When receiving webhooks from external services, signature verification ensures the payload comes from the claimed sender and hasn't been tampered with. Services like Stripe and GitHub include an HMAC signature in headers computed from the request body

N+1 prevention with includes and preload

Eager loading associations with includes, preload, or eager_load is essential for avoiding N+1 queries that kill performance. When rendering a list of posts with their authors, Post.includes(:author) loads all authors in a second query rather than fir

Model validations for data integrity

Validations ensure data consistency before persisting to the database, catching invalid states early in the request lifecycle. I combine presence validations for required fields, uniqueness constraints that map to database indexes, and format validati

Database connection pooling configuration

Properly configured connection pools prevent ActiveRecord::ConnectionTimeoutError during traffic spikes while avoiding resource waste. The pool size should match your application's concurrency needs—for Puma with 5 threads per worker, I set pool: 5 in

API request logging for debugging and analytics

Comprehensive request logging provides visibility into API usage patterns, performance bottlenecks, and security incidents. I log structured JSON that includes request method, path, parameters (sanitized to exclude passwords), response status, duratio

Counter cache for association counts

Computing counts on associations can be expensive when done repeatedly—post.comments.count executes a SELECT COUNT(*) query every time. Counter caches solve this by maintaining a denormalized count column on the parent model that increments/decrements

Optimistic locking for concurrent updates

When multiple users can update the same record simultaneously, optimistic locking prevents lost updates by detecting conflicts. Rails provides built-in support via a lock_version integer column that increments on every update. If two users load the sa

Serializers with ActiveModel::Serializers

Controllers shouldn't know about JSON structure—that's a serialization concern. ActiveModel::Serializers (AMS) separates presentation from business logic by defining dedicated serializer classes for each model. Serializers specify exactly which attrib

Query objects for complex ActiveRecord queries

When queries become too complex for scopes—involving multiple joins, subqueries, or raw SQL fragments—I extract them into dedicated query objects. Each query object is a plain Ruby class that encapsulates one specific query pattern and returns an Acti

ActiveRecord callbacks for lifecycle hooks

Callbacks hook into the ActiveRecord lifecycle to execute code before or after operations like create, update, or destroy. I use before_validation to normalize data (like downcasing emails), after_create to trigger welcome emails, and before_destroy t

Strong parameters for mass assignment protection

Strong parameters prevent mass assignment vulnerabilities by explicitly whitelisting which attributes can be set via user input. Without this protection, attackers could modify sensitive fields like admin or account_balance by including them in reques

Database transactions for data consistency

Transactions ensure that multiple database operations either all succeed or all fail together, preventing partial updates that leave data in inconsistent states. Rails provides ActiveRecord::Base.transaction which wraps a block of code in a database t