Skip to content

Commit 5ecb788

Browse files
jonathanhefnerradareileencodesp8tlatsas
committed
Edit GS guide § Database Migrations [ci-skip]
Fixes #39920. Resolves #38288. Resolves #40040. Co-Authored-By: Ryan Bigg <me@ryanbigg.com> Co-Authored-By: Eileen M. Uchitelle <eileencodes@gmail.com> Co-Authored-By: Petrik de Heus <petrik@deheus.net> Co-Authored-By: Tasos Latsas <tlatsas2000@gmail.com>
1 parent f492209 commit 5ecb788

File tree

1 file changed

+55
-57
lines changed

1 file changed

+55
-57
lines changed

guides/source/getting_started.md

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,61 @@ The two files we'll focus on are the migration file
410410
(`db/migrate/<timestamp>_create_articles.rb`) and the model file
411411
(`app/models/article.rb`).
412412

413+
### Database Migrations
414+
415+
*Migrations* are used to alter the structure of an application's database. In
416+
Rails applications, migrations are written in Ruby so that they can be
417+
database-agnostic.
418+
419+
Let's take a look at the contents of our new migration file:
420+
421+
```ruby
422+
class CreateArticles < ActiveRecord::Migration[6.0]
423+
def change
424+
create_table :articles do |t|
425+
t.string :title
426+
t.text :body
427+
428+
t.timestamps
429+
end
430+
end
431+
end
432+
```
433+
434+
The call to `create_table` specifies how the `articles` table should be
435+
constructed. By default, the `create_table` method adds an `id` column as an
436+
auto-incrementing primary key. So the first record in the table will have an
437+
`id` of 1, the next record will have an `id` of 2, and so on.
438+
439+
Inside the block for `create_table`, two columns are defined: `title` and
440+
`body`. These were added by the generator because we included them in our
441+
generate command (`bin/rails generate model Article title:string body:text`).
442+
443+
On the last line of the block is a call to `t.timestamps`. This method defines
444+
two additional columns named `created_at` and `updated_at`. As we will see,
445+
Rails will manage these for us, setting the values when we create or update a
446+
model object.
447+
448+
Let's run our migration with the following command:
449+
450+
```bash
451+
$ bin/rails db:migrate
452+
```
453+
454+
The command will display output indicating that the table was created:
455+
456+
```
457+
== CreateArticles: migrating ===================================
458+
-- create_table(:articles)
459+
-> 0.0018s
460+
== CreateArticles: migrated (0.0018s) ==========================
461+
```
462+
463+
TIP: To learn more about migrations, see [Active Record Migrations](
464+
active_record_migrations.html).
465+
466+
Now we can interact with the table using our model.
467+
413468
Getting Up and Running
414469
----------------------
415470

@@ -718,63 +773,6 @@ This action is now displaying the parameters for the article that are coming in
718773
from the form. However, this isn't really all that helpful. Yes, you can see the
719774
parameters but nothing in particular is being done with them.
720775

721-
### Running a Migration
722-
723-
As we've just seen, `bin/rails generate model` created a _database migration_ file
724-
inside the `db/migrate` directory. Migrations are Ruby classes that are
725-
designed to create and modify database tables. Rails uses
726-
rake commands to run migrations, and it's possible to undo a migration after
727-
it's been applied to your database. Migration filenames include a timestamp to
728-
ensure that they're processed in the order that they were created.
729-
730-
If you look in the `db/migrate/YYYYMMDDHHMMSS_create_articles.rb` file
731-
(remember, yours will have a slightly different name), here's what you'll find:
732-
733-
```ruby
734-
class CreateArticles < ActiveRecord::Migration[6.0]
735-
def change
736-
create_table :articles do |t|
737-
t.string :title
738-
t.text :text
739-
740-
t.timestamps
741-
end
742-
end
743-
end
744-
```
745-
746-
The above migration creates a method named `change` which will be called when
747-
you run this migration. The action defined in this method is also reversible,
748-
which means Rails knows how to reverse the change made by this migration,
749-
in case you want to reverse it later. When you run this migration it will create
750-
an `articles` table with one string column and a text column. It also creates
751-
two timestamp fields to allow Rails to track article creation and update times.
752-
753-
TIP: For more information about migrations, refer to [Active Record Migrations]
754-
(active_record_migrations.html).
755-
756-
At this point, you can use a rails command to run the migration:
757-
758-
```bash
759-
$ bin/rails db:migrate
760-
```
761-
762-
Rails will execute this migration command and tell you it created the Articles
763-
table.
764-
765-
```
766-
== CreateArticles: migrating ==================================================
767-
-- create_table(:articles)
768-
-> 0.0019s
769-
== CreateArticles: migrated (0.0020s) =========================================
770-
```
771-
772-
NOTE. Because you're working in the development environment by default, this
773-
command will apply to the database defined in the `development` section of your
774-
`config/database.yml` file. If you would like to execute migrations in another
775-
environment, for instance in production, you must explicitly pass it when
776-
invoking the command: `bin/rails db:migrate RAILS_ENV=production`.
777-
778776
### Saving Data in the Controller
779777

780778
Back in `ArticlesController`, we need to change the `create` action

0 commit comments

Comments
 (0)