Skip to the content.

Rails Basics

Getting started

The most important commands in a rails project are the following:

To generate a new project, run rails new <projectname>. This generates an entire project structure, including database config. You can specify --database=postgresql to use a specific database engine instead of SQLite.

Rest

The Rails convention is that for every resource, the following actions and corresponding URLs exist:

Request flow & Architecture

A request is first handled by the Router file, where URLs are mapped to Controller actions:

config/routes.rb

get '/', to: 'home#index'

resources :posts

This routing file defines that requests to the root url should be handled by the index-Method of the HomeController. Additionally, resources :name creates all the REST urls mentioned above for the named resource, and lets the PostsController handle them.

You can view all defined routes by running the rails routes command.

The controller defines what should happen for an incoming request:

app/controllers/posts_controller.rb

class PostsController < ApplicationController
  def index
    @posts = Post.all()
  end

  def show
    @post = Post.find(params[:id])
  end

  def delete
    post = Post.find(params[:id])
    post.destroy()

    redirect_to(posts_path)
  end
end

After the controller has run, unless a redirect is defined, Rails automatically renders the corresponding template:

app/views/posts/show.html.erb

<h1><%= @post.title %></h1>
<p><%= @post.content %></p>

Database

The current schema of the database can be seen in db/schema.rb.

Changes to the database are made using migrations, which are instructions on how to change the database state to get to the target schema. Migrations can include

To generate a migration, run rails generate migration AddTitleToPosts title:string to automatically generate a migration adding the title attribute to the posts table.

Models and ORM

Models are the interface between the application code and the database. They contain definitions of the relationship between tables, so you can query related records in Ruby, while the database is abstracted away.

The component for the ORM is ActiveRecord.

Creation

Goal Method Example
Creation (with DB write) create() Post.create(title: 'Test', author: 'Me')
Instanciation (no DB write) new() post = Post.new(title: 'Bla')
Save save() post.save()

Query

Goal Method Example
All records all() Post.all()
Single record by ID find() Post.find(3)
Single by other attributes find_by_* Post.find_by(title: 'Test', author_name: 'Bla')
Multiple where() Post.where(likes: 12)

Update

Goal Method Example
Update (with DB write) update() post.update(title: 'Foo')
Update (no DB write) assignment post.title = 'Foo'

Deletion

Goal Method Example
Single deletion destroy() Post.find(3).destroy()
Bulk deletion destroy_all() Post.destroy_all()

You can also query related records, and chain the queries:

Post.find(3).authors.where('followers > 3')

Models also contain validation code, where developers can define constraints for the database fields.