Last Updated: February 25, 2016
·
459
· miguelperez

[rails] modifying column data inside migration

image you want to add a column which data depends on other data previously on the table like:

add_column :tables, :new_row, :string
Table.all.each do |table|
  if table.correct?
    table.new_row = 'yes'
  else
    table.new_row = 'no'
  end
end

That will not work :(

You would need to tell that model to reload its table scheme information first.

add_column :tables, :new_row, :string
Table.reset_column_information
Table.all.each do |table|
  if table.correct?
    table.new_row = 'yes'
  else
    table.new_row = 'no'
  end
end