Ruby on Rails Overview
Ruby on Rails (usually just "Rails") is a full-stack web framework written in Ruby. Released by David Heinemeier Hansson in 2004, it changed web development with its focus on developer productivity, and it still powers GitHub, Shopify, Basecamp, Airbnb's early versions and thousands of startups. This lesson is a conceptual tour — Rails needs a server and a database, so it cannot run in the playground — but the Ruby you have learned is exactly what Rails code is made of.
Two guiding principles
| Principle | Meaning |
|---|---|
| Convention over Configuration | Follow naming conventions and Rails wires things together for you. A Product model maps to a products table automatically. |
| Don't Repeat Yourself (DRY) | Every piece of knowledge lives in one place. Validations sit in the model, not scattered across forms. |
MVC architecture
Rails organises an application into three layers:
| Layer | Folder | Responsibility |
|---|---|---|
| Model | app/models |
Data and business rules (Active Record) |
| View | app/views |
HTML templates (ERB), JSON responses |
| Controller | app/controllers |
Receives a request, talks to models, picks a view |
A request travels like this: browser → router (config/routes.rb) → controller action → model (database) → view → HTML response.
Creating an app
gem install rails
rails new bookshelf
cd bookshelf
bin/rails serverSuccessfully installed rails-8.0.2
create bookshelf
create app/models, app/views, app/controllers ...
run bundle install
Bundle complete! 22 Gemfile dependencies.
=> Booting Puma
=> Rails 8.0.2 application starting in development
* Listening on http://127.0.0.1:3000
Open http://localhost:3000 and the app is running.
Generators and migrations
Rails generators create files that follow the conventions. A scaffold creates a model, a database migration, a controller, views and routes in one go:
bin/rails generate scaffold Book title:string author:string pages:integer
bin/rails db:migrate invoke active_record
create db/migrate/20250301120000_create_books.rb
create app/models/book.rb
invoke resource_route
route resources :books
invoke scaffold_controller
create app/controllers/books_controller.rb
create app/views/books/index.html.erb
== 20250301120000 CreateBooks: migrating ======================================
-- create_table(:books)
== 20250301120000 CreateBooks: migrated (0.0021s) =============================
A migration is a Ruby file describing a change to the database schema:
class CreateBooks < ActiveRecord::Migration[8.0]
def change
create_table :books do |t|
t.string :title
t.string :author
t.integer :pages
t.timestamps
end
end
endModels with Active Record
A model is a plain Ruby class that inherits from ApplicationRecord. Columns become attributes automatically — no attr_accessor needed (metaprogramming at work!):
class Book < ApplicationRecord
belongs_to :author
has_many :reviews, dependent: :destroy
validates :title, presence: true
validates :pages, numericality: { greater_than: 0 }
scope :long, -> { where("pages > ?", 400) }
def short? = pages < 150
endQuerying uses chainable Ruby methods that generate SQL:
Book.where(author: "Le Guin").order(:title).limit(5)
Book.long.count
book = Book.find(12)
book.update(pages: 320)
Book.create!(title: "Kindred", pages: 264)belongs_to, has_many, validates and scope are class-level macros — just like the attr_with_default you wrote in the metaprogramming lesson.
Routes and controllers
# config/routes.rb
Rails.application.routes.draw do
resources :books # 7 RESTful routes: index, show, new, create, edit, update, destroy
root "books#index"
end# app/controllers/books_controller.rb
class BooksController < ApplicationController
def index
@books = Book.order(:title)
end
def create
@book = Book.new(book_params)
if @book.save
redirect_to @book, notice: "Book added"
else
render :new, status: :unprocessable_entity
end
end
private
def book_params = params.require(:book).permit(:title, :author, :pages)
endInstance variables set in a controller action (@books) are available in the view.
Views with ERB
ERB templates mix HTML with Ruby. <%= %> outputs a value; <% %> runs code without output:
<h1>Books</h1>
<ul>
<% @books.each do |book| %>
<li><%= link_to book.title, book %> — <%= book.pages %> pages</li>
<% end %>
</ul>Ruby's standard library includes ERB, so you can try the templating idea right here:
require "erb"
books = [
{title: "Dune", pages: 412},
{title: "Kindred", pages: 264},
]
template = ERB.new(<<~HTML, trim_mode: "-")
<ul>
<%- books.each do |b| -%>
<li><%= b[:title] %> (<%= b[:pages] %> pages)</li>
<%- end -%>
</ul>
HTML
puts template.result(binding)<ul>
<li>Dune (412 pages)</li>
<li>Kindred (264 pages)</li>
</ul>
The wider Rails toolkit
| Component | Purpose |
|---|---|
| Active Record | ORM: models, associations, validations, migrations |
| Action Pack | routing, controllers |
| Action View | templates, helpers, forms |
| Active Job + Solid Queue | background jobs |
| Action Mailer | sending email |
| Action Cable | WebSockets |
| Active Storage | file uploads |
| Hotwire (Turbo + Stimulus) | interactive pages with minimal JavaScript |
| Kamal | deployment to your own servers |
When to use Rails
Rails shines for database-backed web applications — SaaS products, marketplaces, admin tools, APIs — where shipping features quickly matters. For tiny services, lighter frameworks like Sinatra or Roda may be enough. Everything you learned in this course — blocks, hashes, symbols, classes, modules, metaprogramming — is used constantly in Rails code.
Tip: The official Getting Started with Rails guide at guides.rubyonrails.org walks you through building a complete blog application.
Test yourself
What does 'Convention over Configuration' mean in Rails?
- You must configure everything in XML
- Following naming conventions lets Rails connect pieces automatically
- Configuration files are forbidden
- Conventions are optional plugins
In Rails' MVC, which layer holds validations and business rules?
- Model
- View
- Controller
- Router
What is a migration?
- Moving a server to a new host
- A Ruby file that changes the database schema
- A way to upgrade Ruby
- A background job