In order to do a single table inheritance, i added a new field called
‘type’, and set the value of it to either “Course” or “Lesson”, based on
another field called “is_course”, which is boolean.
I set up a callback to set the value of type whenever a record is saved,
and i set all the previously existing records values for ‘type’ with an
SQL update, via the rails helper update_all, eg
Item.update_all(‘type = “Lesson”’) #is_course defaults to 0, ie Lesson
Item.update_all(‘type = “Course”’, ‘is_course = 1’)
That all worked fine, no problems. But it occurred to me that if the db
migration version is rolled back and forward again, ie the ‘type’ column
is removed and then re-added, then the values will be lost. Is there
any way to write the update into the same migration that adds the
column, so that it will be run again if the column has to be re-created?