Ruby on Rails
Ruby on Rails is a full-stack web framework built around one big idea: convention over configuration. Instead of asking you to wire routes to controllers to models, Rails assumes a standard structure — name things conventionally and everything connects itself.
Rails defined the modern web framework. Its patterns (MVC, migrations, RESTful resources, scaffolding) were copied by Django, Laravel, and nearly everything since, and it remains one of the fastest ways for a small team to ship a real product — GitHub, Shopify, Stripe, and Basecamp all run on it.
TL;DR
- Convention over configuration: a
Postmodel maps to apoststable,PostsController, and/postsroutes automatically. - Active Record is the ORM: models are classes, associations are one-liners (
has_many :comments), queries are chainable. resources :postsin routes generates all seven RESTful routes at once.- Migrations version the schema; scaffolding generates model + controller + views in one command.
- The modern default stack is Hotwire (Turbo + Stimulus): server-rendered HTML with SPA-like interactivity and minimal JavaScript.
--apimode strips the view layer for pure JSON APIs.
Quick Example
Generate a working blog resource in two commands:
The pieces that scaffold generated:
Core Concepts
Convention over Configuration
Rails derives the wiring from names. Follow the conventions and you write no glue code:
This is the productivity trade: less flexibility in exchange for never writing configuration for the common case.
Active Record
Models wrap tables; associations and validations are declarative:
includes is the N+1 killer — the same query optimization concern as Django's select_related.
Migrations
Schema changes are versioned Ruby files:
The current schema snapshot lives in db/schema.rb — the same discipline as any database migration workflow.
The Request Cycle (MVC)
Controllers stay thin; business logic belongs in models or plain Ruby service objects (app/services).
The Modern Rails Stack
Hotwire: SPA Feel Without the SPA
Rails' answer to heavy JavaScript frontends is Hotwire:
- Turbo Drive intercepts links/forms and swaps the
<body>— no full page reloads. - Turbo Frames update fragments of a page independently.
- Turbo Streams push server-rendered partials over WebSockets for live updates.
- Stimulus adds small, targeted JavaScript behaviors where needed.
The result: server-rendered HTML with most of a SPA's responsiveness and a fraction of the JavaScript. For React-style frontends, Rails also works cleanly as an API backend.
API Mode
Strips views, cookies, and asset pipeline; controllers render JSON. Pair with jbuilder or serializer gems and you have a conventional REST API backend for a React/React Native frontend.
Batteries You Get for Free
Best Practices
Strong Parameters, Always
Mass assignment is Rails' classic vulnerability: never pass raw params to a model.
Keep Controllers Boring
A controller action should read like: authenticate → load → call one domain method → respond. Extract multi-step business logic into service objects or model methods so it's testable without HTTP.
Test the Rails Way
Rails ships a full test harness (Minitest; many teams use RSpec). Fixtures or FactoryBot build data; system tests drive a real browser:
Watch the N+1s
Rails renders views lazily against Active Record, which makes N+1 queries easy to introduce silently. Use includes, and add the bullet gem in development to flag them.
Rails vs Alternatives
Choose Rails when a small team must ship a database-backed product quickly and Ruby is acceptable; choose Django for Python ecosystems; choose a microframework when the product is a thin API.
Common Mistakes
Fighting the Conventions
Renaming directories, bypassing Active Record, or bolting on foreign architectures forfeits the productivity you chose Rails for. If you disagree with most of Rails' opinions, pick a different framework — don't fight this one.
Fat Models Instead of Fat Controllers
"Skinny controllers, fat models" taken literally produces 2,000-line models. Past a point, extract service objects, form objects, and POROs (plain old Ruby objects).
Ignoring the Schema
Active Record hides SQL well enough that missing indexes go unnoticed until production. Index your foreign keys and everything you where on.
FAQ
Is Rails still relevant?
Yes. Rails 7/8 modernized the stack (Hotwire, Solid Queue/Cache, Kamal deployment), and companies like Shopify and GitHub run some of the world's largest Rails codebases. It's no longer the hype default, but for CRUD-heavy products its velocity is still hard to beat.
Does Rails scale?
Shopify handles Black Friday on Rails. Scaling Rails is conventional engineering: caching, background jobs, read replicas, and horizontal app servers. Ruby's raw speed matters less than architecture — and YJIT has improved it substantially.
Rails or Django?
Nearly identical philosophy; choose by language and ecosystem. Python/Django wins if you'll touch data science or ML; Rails wins on scaffolding speed and the Hotwire full-stack story. Team experience should dominate the decision.
Do I need to know Ruby well first?
Basic Ruby is enough to start — Rails teaches you idiomatic Ruby as you go. Understanding blocks, symbols, and modules helps you read framework code and error messages.
What is Hotwire, in one sentence?
A set of techniques (Turbo + Stimulus) that deliver SPA-like interactivity by sending server-rendered HTML over the wire instead of JSON, minimizing custom JavaScript.
Related Topics
- Ruby — The language underneath
- Django — The Python equivalent
- Laravel — The PHP equivalent
- REST API Design — The resource model Rails routes encode
- Database Migrations — Schema versioning practices
- Background Jobs — Active Job and Sidekiq patterns
- WebSockets — What Action Cable wraps