Other Pages

Expand All

Creating A Migration



    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:

  • Create a a simple Table in the database for topics with a title and a description

  • Automatically generate the corresponding Scaffold in Rails (namely, the Model, the View, and the Controller).


Step 1

Type this in the terminal:
rails generate scaffold topic title:string description:text
  • 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". (What's the difference between "string" and "text"? Basically "text" is for strings that might be very long.)

If you want, take some time to poke around the files listed in this step. You can learn about them in the Rails Architecture page.

Step 2

Type this in the terminal:
rake db:migrate

This tells rails to update the database to include a table for our new model.



rake (ruby make) is a tool that allows you to run small Ruby programs (tasks) that you use often in your application.

Here, rake db:migrate is a task provided by the Rails framework. It creates a bunch of new files, including a migration, a model, a view, and a controller.

You can run rake -T to see a list of all the rake commands your app currently responds to, along with a short description of each task.

Next Step:

Back to Running Your Application Locally

If you find something that could be improved, please make a pull request or drop us a note via GitHub Issues (no technical knowledge required).

Source: https://github.com/RailsBridge-CapeTown