ker*_*lin 0 mysql migration ruby-on-rails
令人费解的是,当我使用rake运行以下迁移代码时,列数而不是值显示在MySQL数据库表中:
class AddTypeToItems < ActiveRecord::Migration
def self.up
add_column :items, 'type', :string, :limit => 100, :null => false
Item.find_by_name('YUMMY_JUICE').update_attribute(:type, 'Juice')
Item.find_by_name('TASTY_JUICE').update_attribute(:type, 'Juice')
Item.find_by_name('NASTY_JUICE').update_attribute(:type, 'Juice')
end
def self.down
remove_column :items, 'type'
end
end
Run Code Online (Sandbox Code Playgroud)
我实际上必须回滚迁移,然后再次运行它(总共两次)以显示值.到底是怎么回事?
尝试添加
Items.reset_column_information
在你的add_column陈述后.ActiveRecord缓存列信息,因此您需要在使用它们之前重新加载.