I need to change the type of a column (wasserstand) from string to integer, which already contains text.
My idea is to copy the string into a new scratch column, modify the column type to integer and then manually set the appropriate ints.
After this, I want to remove the scratch string column in the next migration.
The first migration should be done like this:
def self.up tz = Teilzaehlung.all ws = tz.each do |b| ws << [b.id, b.wasserstand] end
change_column(:teilzaehlungs, :wasserstand, :integer) add_column(:teilzaehlungs, :wst, :string)
ws.each do |b| beob = Teilzaehlung.find(b[0]) beob.update_attribute(:wasserstand, 0) beob.update_attribute(:wst, b[1]) end end
Interestingly, after running the migration, the new interger typed column contains 0, but the scratch column wst is always NULL - even if b[1] contains a string.
Why?