ruby
23 lines · 6 steps
Building a weekly digest email in Rails
A mailer action queries a user's unread notifications, groups them for the view, and skips sending when there's nothing to report.
Explained by
highlit
1class DigestMailer < ApplicationMailer
2 default from: "notifications@example.com"
3
4 def weekly_digest(user)
5 @user = user
6 notifications = user.notifications
7 .unread
8 .includes(:notifiable)
9 .where(created_at: 1.week.ago..Time.current)
10 .order(created_at: :desc)
11
12 @grouped = notifications.group_by { |n| n.created_at.to_date }
13 @channels = notifications.group_by(&:channel)
14 @total = notifications.size
15
16 return if @total.zero?
17
18 mail(
19 to: user.email,
20 subject: "You have #{@total} new #{'notification'.pluralize(@total)}"
21 )
22 end
23end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Chaining scopes with includes avoids N+1 queries when the mail template renders associated records.
- 2Precomputing grouped collections in the action keeps view templates simple and logic-free.
- 3A guard clause lets a mailer opt out of sending entirely when there's no content worth delivering.
Related explainers
ruby
class Order class InvalidTransition < StandardError; end TRANSITIONS = {
A state machine for order transitions in Ruby
state-machine
data-driven
error-handling
Intermediate
8 steps
ruby
class CreateOrderItems < ActiveRecord::Migration[7.1] def change create_table :order_items do |t| t.references :order, null: false, foreign_key: { on_delete: :cascade }
Enforcing order-item integrity in Rails
migrations
foreign-keys
validations
Intermediate
7 steps
ruby
class Registration < ApplicationRecord belongs_to :event validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
Validating registrations in Rails
validations
i18n
error-handling
Intermediate
8 steps
ruby
class ApacheLogParser LINE_PATTERN = /\A(?<ip>\S+)\s\S+\s\S+\s\[(?<time>[^\]]+)\]\s"(?<method>[A-Z]+)\s(?<path>\S+)\s(?<protocol>[^"]+)"\s(?<status>\d{3})\s(?<bytes>\d+|-)/ TIME_FORMAT = "%d/%b/%Y:%H:%M:%S %z"
Parsing Apache logs with named captures
regex
named-captures
parsing
Intermediate
6 steps
ruby
require "csv" def parse_transaction_row(line) fields = CSV.parse_line(line, headers: false, skip_blanks: true)
Parsing one CSV transaction row in Ruby
parsing
type-coercion
error-handling
Intermediate
6 steps
ruby
class NewCommentNotifier < ApplicationNotifier deliver_by :database deliver_by :email do |config|
Multi-channel notifications with Noticed in Rails
notifications
delivery-methods
fan-out
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-weekly-digest-email-in-rails-explained-ruby-500e/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.