Last Updated: February 25, 2016
·
1.304K
· neaf

Avoid ActiveRecord "Unknown column 'table.column' in 'field list'" exception between dropping the column and application restart

If you just run migration with drop column statement ActiveRecord in your app instance will continue to issue queries with non-existing items in column list causing exceptions.

To avoid downtime deploy this initialzier before dropping columns.

model = MyModel
column_names = %w{ first_column second_column }

columns = model.columns.select do |c|
  column_names.include?(c.name)
end

columns.each do |c|
  model.columns.delete(c)
end