ruby

Value objects for domain modeling

Value objects represent immutable domain concepts without identity. I use value objects for money, addresses, date ranges, coordinates. Value objects are compared by value, not identity—two identical addresses are equal. They're immutable—create new i

ActionMailer advanced patterns for transactional emails

ActionMailer handles email delivery in Rails. Mailers are similar to controllers—actions generate email content. I use ActionMailer for welcome emails, password resets, notifications. Layouts apply consistent styling across emails. Previews enable vie

ActiveRecord query optimization and N+1 prevention

ActiveRecord provides powerful query interface, but naive usage causes N+1 queries. includes eager loads associations in 2-3 queries. joins performs SQL JOINs for filtering. preload always uses separate queries; eager_load forces LEFT OUTER JOIN. I us

ActiveStorage for file uploads and attachments

ActiveStorage handles file uploads with cloud storage integration. It supports local disk, S3, Google Cloud Storage, Azure. Files attach to models via has_one_attached and has_many_attached. I use ActiveStorage for avatars, documents, images. Image va

ActionCable for real-time WebSocket communication

ActionCable integrates WebSockets seamlessly with Rails. Channels handle pub/sub messaging between server and clients. I use ActionCable for chat, notifications, live updates. Channels subscribe clients to streams; broadcasting pushes data to subscrib

Custom validators and validation patterns

Rails validations ensure data integrity before persistence. I create custom validators for complex business rules. Validators inherit from ActiveModel::Validator or ActiveModel::EachValidator. EachValidator validates individual attributes; Validator v

Service objects for business logic encapsulation

Service objects extract complex business logic from models and controllers, following Single Responsibility Principle. I structure services with a clear public interface—typically a call method. Services handle multi-step operations, external API call

Ruby refinements for scoped monkey patching

Refinements provide scoped modifications to existing classes without global monkey patching. I use refinements to add methods to core classes safely. Refinements activate with using statement—scope-limited to file or module. Unlike monkey patches, ref

Regular expressions for pattern matching

Ruby's regex engine provides powerful text processing. I use =~ for matching, match for captures. Character classes \d, \w, \s match digits, words, whitespace. Quantifiers *, +, ?, {n,m} control repetition. Anchors ^ and $ match start/end. Groups () c

Database migrations and schema management

Rails migrations evolve database schema over time. I use change method for reversible migrations. Migrations create tables, add/remove columns, add indices. up and down methods provide explicit control. Irreversible migrations like data transformation

Form objects for complex form handling

Form objects encapsulate form logic separate from models. I use form objects for multi-model forms, complex validations, or forms not directly mapping to models. Form objects include ActiveModel modules for validations and callbacks. They handle param

Module mixins and concerns for code reuse

Ruby modules enable code sharing across classes without inheritance. I use include for instance methods, extend for class methods. prepend inserts module before class in method lookup. Concerns organize shared behavior—validations, scopes, association