maeshimaの日記

メモ書きです

migration中に変更したカラムを使いたい場合

ActiveRecord::Base#reset_column_informationを実行して、カラムの情報を一度リセットしないとエラーになってしまう。下記のソースはrailsのドキュメントからの引用。

 class CreateJobLevels < ActiveRecord::Migration
   def self.up
     create_table :job_levels do |t|
       t.integer :id
       t.string :name

       t.timestamps
     end

     JobLevel.reset_column_information
     %w{assistant executive manager director}.each do |type|
       JobLevel.create(:name => type)
     end
   end

   def self.down
     drop_table :job_levels
   end
 end