This is usually a bad idea to reference your models from your migrations. The problem is that the migrations are performed in order and change the state of the database as they arrive, but your models are not versioned at all. There is no guarantee that the model, as it existed when recording the migration, will still be compatible with the migration code in the future.
For example, if you change the behavior of the is_active or is_live in the future, this migration may break. This older migration will be launched first, against the new model code and may fail. In your base example, this may not occur, but it burned me down in the deployment before the fields were added and the checks could not be performed (I know that your code skips the checks, but in general this is a concern).
My favorite solution for this is to do all migrations of this kind using plain SQL. It sounds like you already thought that, so I'm going to assume that you already know what to do there.
Another option, if you have hairy business logic or just want the code to look more Railsy, should include the basic version of the model as it exists when the migration is written to the migration file itself. For example, you can put this class in a migration file:
class Group < ActiveRecord::Base end
In your case, this is probably enough to ensure that the model does not break. Assuming that active and live are Boolean fields in the table at this time (and therefore, this will be whenever this migration starts in the future), you will no longer need the code. If you had more complex business logic, you can include it in this version of the migration-centric model.
You may even consider copying all the methods from your model into the migration version. If you do this, keep in mind that you should also not reference any external models or libraries of your application, if there is a possibility that they will change in the future. This includes gems and maybe even some of the core Ruby / Rails classes, because changes to gems are a violation of the APIs (I'm looking at you, Rails 3.0, 3.1, and 3.2!).
Jim stewart
source share