ActiveStorage: variants
This course follows how a Rails app handles images with ActiveStorage, using the app's own code. It explains variants as lazy transformation recipes processed by libvips, and how has_one_attached and has_many_attached differ, including why assignment replaces images while .attach adds them. It walks through an upload optimizer that converts images to WebP, caps dimensions, sets quality and strips EXIF data, and shows where that optimizer runs. It also covers proxy delivery versus signed redirects, readable lesson image URLs and N+1 queries on attachments.
What you'll learn
- ✓How variants work as lazy recipes on a single blob, processed by vips
- ✓The difference between has_one_attached and has_many_attached, and why .attach is safer than =
- ✓How an optimizer converts uploads to WebP, caps size, sets quality and strips EXIF
- ✓Why optimization runs synchronously in the admin request and how blobs get random filenames
- ✓How proxy mode serves stable URLs compared with expiring signed redirects
- ✓How to avoid N+1 queries on attachments with with_attached_ or a direct Preloader
What a variant is
A profile photo usually needs to appear in several shapes: a tiny thumbnail in a list, a medium preview on a card, a full-size crop on the profile itself. The naive fix is to upload three files and manage three records. ActiveStorage's answer is a variant: one uploaded file, and as many derived…
Read the full lesson free →Curriculum
7 lessonsWhat a variant is
Variants as lazy transformation recipes on one blob, processed by libvips, with the cost of the first request.
Attaching one image, or many
How has_one_attached and has_many_attached differ, and why assigning with = replaces images while .attach adds them.
This app's image optimization pipeline
A service that converts uploads to WebP, caps size, strips EXIF data and swaps the blob in place.
Where optimization actually gets triggered
The admin controller runs optimization synchronously in the upload request, then renames blobs with random hex filenames.
Redirect or proxy, and this app's URL obfuscation
Why this app serves files through a Rails proxy instead of expiring redirects, plus readable lesson image URLs.
N+1 and attachments
How attachments cause N+1 queries, and when the app uses with_attached_logo or a direct Preloader instead.
Putting it together: one image, start to finish
One upload traced through every step covered, and why the course ships without a coding challenge.