Ensure Migrations Use The Latest Schema
Real-world migrations in Rails apps can sometimes involve both schema changes and data changes. For instance, if you are moving a column from one table to another, you'll need to add a new column, move some data, and then delete the old column.
This migration looks straightforward, but you may find that no data actually gets transferred to the genre
column on books
. This is because as a performance optimization, Rails has cached the scema. Thus an ActiveRecord
modification like book.update!
will be working off a version of the schema that doesn't include genre
as a column.
We can ensure ActiveRecord
is using the latest column informtion for the books
table by calling reset_column_information
.
Now the update will work and genre
will be set on books
.
Last updated