So you’ve decided to rename a model in Rails. What could be simpler than renaming and editing a bunch of files? You make your edits in development, run migrations and so on.
There’s a potential issue you might hit though. Suppose you have model Foo, which you want to modify (e.g. add a column). So you create a migration with:
add_column :foo, :quantity, :integer
…and you run your migration.
Then you decide you want to rename the model to Bar. So you use rename_table in your migration, and rename and edit the appropriate files. All works beautifully.
Now you come to deploy, and it all goes wrong when you migrate the database, saying it can’t find the model. The reason? Well your first migration now refers to a model that doesn’t exist, because the new model file is in place.
The messy solution is to perform the migration yourself, by renaming the database table directly in the database.
The clean solution is to avoid making changes like this in the first place. I guess it’s an example of where “deploy often” can reap benefits.
-
Archives
- January 2020
- August 2018
- August 2017
- July 2015
- February 2015
- February 2013
- December 2011
- August 2011
- May 2011
- April 2011
- January 2011
- November 2010
- September 2010
- August 2010
- January 2010
- November 2009
- September 2009
- July 2009
- June 2009
- May 2009
- July 2008
- June 2008
- February 2008
- January 2008
- December 2007
- October 2007
- July 2006
- January 2002
- November 2001
- October 2001
- September 2001
- February 2001
- January 2001
-
Meta