Test Your Knowledge
Quiz
How do you run migrations forward?
How do you create a new migration?
What should the
self.down
method do in a migration?What Rails data type should you use to represent currency values?
How do you add a new field to a record?
Answers
With the
rake db:migrate
command.To keep in line with Railsâ timestamp-based naming convention, itâs best to use
script/generate migration
NameOfMigration
, and then edit the resulting file in the db/migrate directory.The
self.down
method defines what should happen if the migration is rolled back usingrake db:rollback
. The tables, column, and indexes created inself.up
need a corresponding removal process inself.down
.The
:decimal
type is the most precise way to keep track of money. It can keep track of cents to the right of the decimal point and will contain values with a fixed number of decimal places much more accurately than:float
.New fields get created with
add_column
, as âfieldsâ are represented as columns and the records that contain them as rows.
Get Learning Rails: Live Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.