Test Your Knowledge
Quiz
How do you run migrations forward?
How do you create a new migration?
What should the
self.downmethod 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:migratecommand (ordb:migratein the Rake window on Heroku).To keep in line with Rails’ timestamp-based naming convention, it’s best to use
script/generate migrationNameOfMigration, and then edit the resulting file in the db/migrate directory.The
self.downmethod defines what should happen if the migration is rolled back usingrake db:rollback. The tables, column, and indexes created inself.upneed a corresponding removal process inself.down.The
:decimaltype 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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access