ruby
15 lines · 6 steps
Normalizing user input in Rails models
How a Rails model cleans up attributes before validating them.
Explained by
highlit
1class User < ApplicationRecord
2 has_secure_password
3
4 normalizes :email, with: ->(email) { email.strip.downcase }
5 normalizes :phone, with: ->(phone) { phone.gsub(/\D/, "") }
6 normalizes :username, with: ->(username) { username.strip.gsub(/\s+/, "_").downcase }
7 normalizes :website, with: ->(url) { url.blank? ? url : url.sub(%r{\Ahttps?://}i, "") }
8
9 validates :email, presence: true, uniqueness: true,
10 format: { with: URI::MailTo::EMAIL_REGEXP }
11 validates :phone, length: { is: 10 }, allow_blank: true
12 validates :username, presence: true, uniqueness: true,
13 length: { in: 3..30 },
14 format: { with: /\A[a-z0-9_]+\z/ }
15end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Normalizing attributes before validation means your rules check clean data, not raw input.
- 2Pairing normalization with a matching validation prevents subtle mismatches like case-sensitive uniqueness bugs.
- 3Declarative model macros keep data-cleaning logic centralized instead of scattered across controllers.
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
php
<?php namespace App\Http\Controllers;
Streaming a filtered CSV export in Laravel
streaming
csv-export
query-builder
Intermediate
9 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
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/normalizing-user-input-in-rails-models-explained-ruby-13e8/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.