-
Notifications
You must be signed in to change notification settings - Fork 451
/
Copy pathcreating_a_migration.step
63 lines (46 loc) · 2.18 KB
/
creating_a_migration.step
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
goals {
model_diagram header: 'Topics', fields: %w(id title description)
message "The suggestotron has a list of topics that people can vote on. We'll store our topics in the database. In this step you'll do the following:"
goal "Create a simple *Table* in the database for topics with a title and a description"
goal "Automatically generate the corresponding *Scaffold* in Rails (namely, the *Model*, the *View*, and the *Controller*)."
}
steps {
step {
console "rails generate scaffold topic title:string description:text"
message <<-MARKDOWN
* `generate scaffold` tells Rails to create everything necessary to get up and running with topics.
* `topic` tells Rails the name of the new model.
* `title:string` says that topics have a title, which is a "string".
* `description:text` says that topics have a description which is a "text". A "text" is like a "string" that might be very long.
MARKDOWN
}
step {
console "rails db:migrate"
message "This tells Rails to update the database to include a table for our new model."
}
}
explanation {
h2 "Databases don't create tables by themselves - they need instructions."
message <<-MARKDOWN
One of the files the scaffold command created will be named something like
`db/migrate/20140515115208_create_topics.rb`. The numbers in the name are
the date and time the file was made, so yours will be named differently.
The file contains Ruby code that describes what the table will look like.
This is called a migration file. It tells the database how to transform
itself into the new configuration.
```ruby
class CreateTopics < ActiveRecord::Migration
def change
create_table :topics do |t|
t.string :title
t.text :description
t.timestamps
end
end
end
```
`rails db:migrate` is a task provided by the Rails framework. It uses the migration file we just created (`db/migrate/201xxxxxxxxxxx_create_topics.rb`) to change the database.
MARKDOWN
tip "You can run `rails -T` to see a list of all the `rails` commands your app currently responds to, along with a short description of each task."
}
next_step "CRUD_with_scaffolding"