把一个老项目中的 state_machines 全部替换掉,使用 AASM enum,因为字段类型变了,字段名称不想变(强迫症犯了)。
class ReplaceStateMachines < ActiveRecord::Migration
def change
[Order, Customer].each do |clazz|
ActiveRecord::Base.connection.execute("select * from #{clazz.table_name}").each do |r|
ActiveRecord::Base.connection.execute("update #{clazz.table_name} set state = #{clazz.states[r["state"]]} where id = #{r["id"]}")
end
ActiveRecord::Base.connection.execute("ALTER TABLE #{clazz.table_name} ALTER COLUMN state TYPE integer USING (trim(state)::integer)")
end
end
end