add_advertisements_viewĮnd def down SoftDelete :: DropAllViews. drop_advertisements_viewĪdd_column :advertisements, :product_version_id, :integerĪdd_index :advertisements, :product_version_id SoftDelete :: AddAllViews. Using the example from above again:Ĭlass AddProductVersionIdToAdvertisements < ActiveRecord :: Migration def up SoftDelete :: DropAllViews. In most cases, change accomplishes alone what up and down do together. It’s newer to Rails and the primary way to write migrations. Simply, up runs the migration, down rolls the migration back.īut what about change? As I mentioned above, the change method is more commonly used. When you run rails db:migrate, the up method is executed, whereas rails db:rollback executes the down method. In other words, the changes in up are the forward change you want to make to your db, where the changes defined in down should be changes to revert back in time. It is wise to perform the transformations in precisely the reverse order they were made in the up method. For example, if you create a table in the up method, you should drop it in the down method. In other words, the database schema should be unchanged if you do an up followed by a down. The up method should describe the transformation you’d like to make to your schema, and the down method of your migration should revert the transformations done by the up method. You can also use the old style of migration using up and down methods instead of the change method. So what exactly do they do? The Rails guide best explains it: If you’re new to Rails, or have perhaps only worked with more modern versions, up and down might not be familiar. Remove_column :downloads, :category_ids end endĭef up and def down? What’s up with that? What Goes Up Must Come Down Rename_column :downloads, :temporary_category_names, :category_names Rename_column :downloads, :category_names, :temporary_category_names end def down Class RenameCategoryNamesToCategoryIdsInDownloads < ActiveRecord :: Migration def upĪdd_column :downloads, :category_ids, :text
0 Comments
Leave a Reply. |