Single-page version

Senior Track · Spot the Tax · stepped (test)

Working code that will cost you.

Same five snippets as the main page. This time each card walks you through the lesson in three steps. Look at the code, take a guess, then reveal the hint, the solution, and the principle.

How to use this page. For each card, read the code and ask yourself "what's wrong with this in six months?" Click Reveal the hint to see whether you spotted the problem. Then walk through the solution and the underlying principle.

Card 1

The validation that lies under load

What will this cost you in six months?

class User < ApplicationRecord
  validates :email, presence: true, uniqueness: true
end

# In the controller:
def create
  @user = User.new(user_params)
  if @user.save
    redirect_to @user
  else
    render :new
  end
end

Card 2

The callback that takes the database hostage

What will this cost you in six months?

class User < ApplicationRecord
  before_save :sync_to_mailchimp

  private

  def sync_to_mailchimp
    Mailchimp::Subscribers.upsert(
      email: email,
      merge_fields: { name: name }
    )
  end
end

Card 3

The cascade that runs in Ruby

What will this cost you in six months?

class User < ApplicationRecord
  has_many :events, dependent: :destroy
  has_many :messages, dependent: :destroy
  has_many :reactions, dependent: :destroy
end

# In the controller:
def destroy
  current_user.destroy
  redirect_to root_path
end

Card 4

The schema you can't query

What will this cost you in six months?

class Order < ApplicationRecord
  serialize :metadata, JSON
end

# Writing:
order.metadata = { plan: "pro", referrer: "google", utm_campaign: "spring" }
order.save

# Reading later, in a report:
Order.where("created_at > ?", 90.days.ago).select do |o|
  o.metadata["plan"] == "pro"
end

Card 5

The find that trusts the URL

What will this cost you in six months?

class InvoicesController < ApplicationController
  before_action :authenticate_user!

  def show
    @invoice = Invoice.find(params[:id])
  end
end

If the stepped flow worked for you

Tell me. If it lands, the same shape can roll across the SOLID, senior, and DDD cards from the main page.

← Single-page version Senior Track · Spot the Tax · stepped (test)