Skip to content

Latest commit

 

History

History
94 lines (62 loc) · 2.86 KB

README.md

File metadata and controls

94 lines (62 loc) · 2.86 KB

Session 2 - Models

We take a closer look at Model of the MVC architecture and talk about databases, migrations and working with records.

The model layer is responsible for storing and processing data.

We store data in a relational database and process it in the app/models of the Rails application.

A relational database stores information as a set of tables with columns and rows (records). The tables and their columns are together called a schema. You can think of relational database as a spreadsheet with each table on a different sheet.

There are other non-tabular databases as well, which are better suited to specific problems: What is a Database | Oracle

Structured Query Language (SQL) is used to access and manipulate databases. SQL can retrieve, create, read, update and destroy records, modify schema and more. Working with SQL directly is difficult, so we usually have a Rails equivalent.

The assignment is split into different sub-tasks, each testing a different aspect of Model layer.

We will be using SQLite as our database program, as it requires no setup and is Rails's default.

Pre-requistes

Prepare Local Database

  • Change directory to rails project.

  • Create the database

rails db:create

You should see two new files are created if not present already - development.sqlite3 and test.sqlite3.

  • Run the migrations
rails db:migrate

Migrations are a convienient way to alter database schema (that is, modify the tables and the columns) using a ruby-like language.

Creating Tables

Relational databases stores information using tables. You can think of tables and their columns as the format in which data is stored.

In this sub-task, we will build a website to keep track of enrolled students. Head over to Student Registry directory to learn more.

Working with Records

Once a table is created, we have to fill it with actual data. In particular, we can create, read, update and destroy records in a table. Each operation maps to a different SQL command and a different Rails equivalent.

In this sub-task, we will work on statistics from Cricket and hope to crack the problem of winning the fourth test match in Border-Gavaskar series! Head over to Cricviz directory to learn more.

Interactive Console

The Rails console is useful for testing out quick ideas with code and debugging applications.

rails console

This should open a console, similar to IRB in the first session. We can access your model functions and execute any valid ruby code.

Interactive Console