Introduction to Rails
Ruby on Rails is the web framework that made Ruby famous. Created by David Heinemeier Hansson in 2004, Rails follows convention over configuration โ you write less code because sensible defaults handle the details. It's MVC, it's RESTful, and it's incredibly productive.
# Create a new Rails app
# rails new myapp
# Generate a scaffold
# rails generate scaffold Post title:string body:text
# Run migrations
# rails db:migrate
# Start the server
# rails server
Try it Yourself ->
Convention Over Configuration
Rails makes assumptions instead of requiring configuration. If your model is called User, Rails assumes it maps to a users table with columns named after the model's attributes. You don't write XML configs or boilerplate โ you just follow the conventions.
# Rails conventions:
# Model: User -> users table
# Controller: UsersController -> app/controllers/users_controller.rb
# View: index.html.erb -> app/views/users/index.html.erb
# Route: /users -> UsersController#index
# No config needed for:
# - Database connection (config/database.yml)
# - Routing (config/routes.rb)
# - View templates (app/views/)
Try it Yourself ->
MVC Pattern
Rails follows Model-View-Controller. Models handle data and business logic. Views handle presentation. Controllers connect them. This separation keeps code organized and maintainable.
# app/models/post.rb
class Post < ApplicationRecord
validates :title, presence: true
validates :body, presence: true
scope :published, -> { where(published: true) }
end
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.published
end
def show
@post = Post.find(params[:id])
end
end
# app/views/posts/index.html.erb
# <% @posts.each do |post| %>
# <h2><%= post.title %></h2>
# <p><%= post.body %></p>
# <% end %>
Try it Yourself ->
ActiveRecord
ActiveRecord is Rails' ORM (Object-Relational Mapping). It maps Ruby objects to database tables. You interact with your database using Ruby methods instead of SQL. It handles validations, callbacks, associations, and queries.
# Define a model
class User < ApplicationRecord
has_many :posts
has_one :profile
validates :email, presence: true, uniqueness: true
validates :name, length: { minimum: 2 }
end
# Use it in code
user = User.create(name: "Alice", email: "alice@example.com")
user.posts.create(title: "Hello World")
# Query with friendly methods
User.where(name: "Alice").first
User.order(:created_at).limit(10)
User.joins(:posts).where(posts: { published: true })
Try it Yourself ->
Why Rails Made Ruby Famous
Rails showed the world that web development could be fast and enjoyable. It introduced concepts like RESTful routing, database migrations, and scaffolding that became industry standards. Rails is opinionated but productive โ it makes decisions so you don't have to. That's why startups and enterprises alike chose Rails.
# config/routes.rb
Rails.application.routes.draw do
resources :posts do
member do
post :publish
end
collection do
get :drafts
end
end
root "posts#index"
end
# This single file generates:
# GET /posts -> index
# GET /posts/new -> new
# POST /posts -> create
# GET /posts/:id -> show
# GET /posts/:id/edit -> edit
# PATCH /posts/:id -> update
# DELETE /posts/:id -> destroy
# POST /posts/:id/publish -> publish
# GET /posts/drafts -> drafts
Try it Yourself ->