Other Pages

Expand All

creating_a_migration.step

goals do

  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*)."

end

steps do

  step do
    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
    message "If you want, take some time to poke around the files listed in this step. You can learn about them in the [Rails Architecture](rails_architecture) page."
  end

  step do
    console "rails db:migrate"
    message "This tells Rails to update the database to include a table for our new model."
  end
end

explanation do
  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/20170802211140_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[5.1]
      def change
        create_table :topics do |t|
          t.string :title
          t.text :description

          t.timestamps
        end
      end
    end
    ```

    See how this lines up with the scaffold command?

    ```
    rails generate scaffold topic title:string description:text
    ```

    The migration says to create a table named "topics", with a title column
    that's a string, and a description column that's text. Rails automatically
    adds timestamps, and will use it to automatically record when the data was
    created and updated.
  MARKDOWN

  h2 "Rake tasks"
  message <<-MARKDOWN
    **rake** _(ruby make)_ is a tool that allows you to run small Ruby programs (**tasks**) that you use often in your application.

    Here, `rails db:migrate` uses a Rake task provided by the Rails framework. It uses the migration file we just created (`db/migrate/201xxxxxxxxxxx_create_topics.rb`) to change the database. Database migration files can be crucial to code collaboration.

    In previous versions of Ruby on Rails, this command would have been run like so: `rake db:migrate`. Since many developers found it confusing to have some commands run with `rails` (`rails generate`, `rails server`) and some with `rake` (`rake db:migrate`, `rake -T`) they were consolidated. Rake still exists outside of Rails and you may see it from time to time.
  MARKDOWN

  tip "You can run `rails -T` to see a list of all the Rake tasks your app currently responds to, along with a short description of each task."
end


next_step "CRUD_with_scaffolding"