Rails Migrations and AR Models — be careful my friend

Be careful when using AR models in migrations. Many of us know this and some know the workaround of defining your model in the migration, as documented in the aging Rails Recipes book by Chad Fowler in the recipe “Safely Use Models in Migrations.”

As the recipe specifies, you may need to refresh the model’s column data, with the “reset_column_information” class method.

I was caught by surprise when I did this on models that extended a base class using single table inheritance. My first try was to reset the column information of the base class.

Nothing complained. The data was simply not what I expected. After a while I decided to reset the column information for every sub class — voilĂ , success.

Advertisements