पाठ 22 / 25

A First Look at Ruby on Rails

Understand how Rails uses Ruby and what its main parts do.

Convention over configuration

Ruby on Rails is a full-stack web framework built on the principles of convention over configuration and don't repeat yourself. It follows the MVC pattern: models inherit from ApplicationRecord and use Active Record to map classes to database tables, with validations, associations (has_many, belongs_to) and migrations for schema changes; controllers handle requests and choose responses; views render HTML with ERB templates, enhanced in modern Rails by Hotwire (Turbo and Stimulus) for interactive pages with little custom JavaScript. Rails includes background jobs (Active Job, with Solid Queue in recent versions), mail (Action Mailer), WebSockets (Action Cable), file uploads (Active Storage), caching and strong security defaults such as CSRF protection and parameter filtering. Generators (rails generate model Order total:decimal) and the console (rails console) speed up work. Everything in this course appears in Rails: blocks, DSLs, modules, metaprogramming and conventions. Ruby on Rails has its own course in this catalogue.

Rails MVC

Requests are routed to controllers, which use models and render views.

A request arrow into a router icon, then into a controller box connected to a database-backed model box and a page template box.
Figure 8.1 — Routes, controllers, models and views.

A model, a controller and routes

Ruby DSLs everywhere: associations, validations and routes.

# app/models/order.rb
class Order < ApplicationRecord
  belongs_to :customer
  has_many :line_items, dependent: :destroy

  enum :status, { placed: 0, paid: 1, shipped: 2, cancelled: 3 }
  validates :total_paise, numericality: { greater_than: 0 }

  scope :recent, -> { where(created_at: 30.days.ago..) }
end

# app/controllers/orders_controller.rb
class OrdersController < ApplicationController
  def index
    @orders = current_customer.orders.recent.includes(:line_items).order(created_at: :desc)
  end

  def create
    @order = current_customer.orders.build(order_params)
    if @order.save
      redirect_to @order, notice: "Order placed"
    else
      render :new, status: 422
    end
  end

  private

  def order_params = params.require(:order).permit(:total_paise, line_items_attributes: %i[sku quantity])
end

# config/routes.rb
Rails.application.routes.draw do
  resources :orders, only: %i[index show new create]
end

includes avoids N+1 queries

@orders.each { |o| o.line_items.size } without includes(:line_items) runs one extra query per order. Eager loading with includes loads associations in a constant number of queries.

त्वरित जाँच: What does Active Record do in Rails?

  • Renders HTML templates
  • Handles routing
  • Maps Ruby classes to database tables and provides queries, validations and associations
  • Manages JavaScript
Answer

Maps Ruby classes to database tables and provides queries, validations and associations — Active Record is Rails' ORM layer for models.