1. Introduction to Databases
Introduction to working with data through databases
Guides, tutorials and labs to accompany CMU's Programming for Online Prototypes (49-714).
Everything you need to know about building microservices for the web with Ruby and Sinatra.
Did you notice we also got a new file in our db
directory called schema.rb
. This is a running log of the structure of our database or at least as sinatra sees it. It’s updates every time a new migration is applied to our database. And it’s a really useful resource for seeing where things are at with our SQL database.
It’s the authoritative source for your database schema. Active Record generates db/schema.rb
by examining the database to represent it’s current state.
Schema files are useful if you want a quick look at what attributes an Active Record object has. This information is not in the model’s code and is frequently spread across several migrations, but the information is nicely summed up in the schema file. The annotate_models gem automatically adds and updates comments at the top of each model summarizing the schema if you desire that functionality.
A schema looks something like this:
ActiveRecord::Schema.define(version: 20161101024543) do
create_table "lists", force: :cascade do |t|
t.string "name"
end
create_table "tasks", force: :cascade do |t|
t.string "name"
t.integer "list_id"
t.boolean "is_complete", default: false
end
end
You’ll notice that:
The version will match a number from one of your migrations. This tells ActiveRecord if there are migrations that haven’t been applied. And this is also why there can’t be two migrations with the same ID number.
Each of the tables are listed and we can see what columns exist for them.
This series guides you through working with databases, from setting up and installing your database, storing and retrieving data and deploying to heroku
Introduction to working with data through databases
Getting started - installing your database engine
Getting started - adding dependencies to your project
Getting started - adding activerecord rake commands
Migration files are small Ruby scripts that make changes to your database
Schemas define the structure of your database table and allow ActiveRecord to structure requests and changes for data
Use Models to map a database table onto native ruby objects.
Linking Models to Routes. Making a CRUD API
Linking Models to Models - Adding Associations
How to deploy your project to Heroku and add a database
How to check the information you're adding to the database
How to create initial data with a Seed File