How To Use Models In Your Migrations

Most often, ActiveRecord migrations are used to manage database schema changes. Any migration you write should meet one, steadfast rule:

The code in a migration should be able to run, in sequence, at anytime.

Sometimes, however, the data in the database needs to change as a result of a migration. It is tempting to use models inside a migration to affect the data change. This can be done, but you should consider that when external code (your models) is used in a migration, it ties the migration to code that may and most likely will change over time; violating the above rule.

Read More