// TOPIC INDEX Category: #ruby-on-rails

Ruby on Rails

10 Field Notes

Hands-on Rails and Ruby techniques from client work: architecture patterns, testing, mailers, background jobs and performance.

Subscribe Topic RSS
Tags:
Showing 10 of 10 posts in Ruby on Rails
01 // Ruby on Rails

To ViewComponent or not to ViewComponent

GitHub's ViewComponent claims to be about 10x faster than Rails partials. Running its benchmarks shows regular components are roughly 5x faster, and adding memory benchmarks shows partials use at least 3x more memory. A real gain, worth weighing against a new directory structure and extra files.

6 min read
02 // Ruby on Rails

Rails: Join on polymorphic association

A scoped belongs_to makes it possible to join through a polymorphic association in ActiveRecord, but adding one broke unrelated tests with a confusing FactoryBot error. Because belongs_to is required by default since Rails 5, the scoped association needs optional: true.

3 min read
03 // Ruby on Rails

Rails: I18n in mailer layout

Showing an email's subject as a heading in a shared Rails mailer layout sounds trivial, but lazy-lookup translations don't resolve the right scope inside layouts. Digging into ActionMailer's default_i18n_subject points to the fix: build the scope from the mailer name and action_name yourself.

3 min read
04 // Ruby on Rails

Rails: Start multiple processes with one command in development

Real Rails apps depend on several processes in development: the web server, Sidekiq, Mailcatcher, Webpack. Instead of starting each by hand, use a Procfile.dev with Foreman, then wrap it in a bin/server script with flags, so every app on the team starts the same way.

6 min read
05 // Ruby on Rails

Rails: Non blocking analytics

Adding analytics calls inline in controllers makes users wait on third-party APIs and slows the whole app. The fix is to move every analytics call into a background job, and, once you send data to several services, wrap them behind a single adapter, which is what B&G's open-source bg-common gem does.

3 min read
06 // Ruby on Rails

Intro to Trailblazer - Part II: Operations

"Skinny controllers, fat models" pushed business logic into ActiveRecord models that were never meant to hold it. Trailblazer operations give each user interaction, like uploading a document, its own class, keeping controllers thin, models as plain data objects, and side effects explicit.

11 min read
07 // Ruby on Rails

Introduction to Trailblazer Through Decorators

MVC leaves presentation logic with nowhere good to live, so it leaks into views and models. The Decorator pattern fixes that, and Trailblazer's Disposable::Twin makes decorators easy: expose only the properties a view needs, including nested ones, while keeping sensitive data out.

6 min read
08 // Ruby on Rails

Dynamic Hash in Ruby

Seed data built from a shared defaults hash works until one value needs to be unique per record, and suddenly every seeded user shares the same Payment. The fix, without monkey-patching Hash: store a lambda as the value and call it as each record is created.

6 min read
09 // Ruby on Rails

Rails: Email Previews

Rails mailer previews are easy until your emails need realistic data. Four approaches, from querying the database to FactoryGirl, rolled-back transactions and plain OpenStruct objects, and the trade-offs of each.

5 min read
10 // Ruby on Rails

File Upload: Rails + Refile + Dropzone

A start-to-finish walkthrough of building drag-and-drop document uploads in Rails without a frontend framework: Cucumber scenarios first, then the UI, then the backend. It uses Refile for storage, DropzoneJS for drag and drop, and a remote form so the task list updates without a page reload.

16 min read
ESC

Type to search 38 posts