What we're going to do:
- Review what a database is
- Create a table in the database for jobs
- Learn what a migration file is
Discussion: Databases & SQL
Review how relational databases are structured, how we communicate with them (hint: SQL!).
Make a jobs model & migration
In order to make it possible for users to create jobs, we need:
- A place to store the jobs (a table in the database)
- Rails to know what a job is (a model)
We're going to use another Rails generator; this time to make our migration and model!
Type this in the terminal:rails g model job
(The g stands for generate, which has many more letters than is necessary to type.)
That generated two files: a migration and models/job.rb.
Make that migration file useful
Open up the migration file that you just made (cmd+p / ctl+p, type 'create jobs', and hit enter) and you'll see the following:
class CreateJobs < ActiveRecord::Migration[5.0]
def change
create_table :jobs do |t|
t.timestamps
end
end
end
Running this code will make a table in our database called jobs. Right now it just has the timestamps (created_at
and updated_at
). What else should a job have? Let's start with a title and description.
Add the title and description so it looks like this:
create_table :jobs do |t|
t.text :title
t.text :description
t.timestamps
end
Now we need to execute this file, so that the database schema gets updated.
Type this in the terminal:rails db:migrate
This tells Rails to run any migration files that haven't already been run at some point in the past.
Discussion: Why do we use migrations?
Talk about the pros and cons of using migrations to update the database, instead of just updating the schema directly. Also, discuss what the database schema is!
(Pro-tip: never update the schema directly.)
Check out the model
The migration we just ran updated the database, but that doesn't mean that we can talk to the database using Ruby yet. Look at the file app/models/job.rb
. The Job
class inherits from ApplicationRecord, so that we can talk to the database with Ruby instead of SQL!
Okay, so we've got some place to store our jobs. But how can we make any? THROUGH THE MAGIC OF FORMS!!!