开发者

Why do I have to run migration twice for values to appear in DB?

Inexplicably, when I run the following migration code using rake, the column, but not the values, appear in the MySQL DB table:

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('NA开发者_如何学运维STY_JUICE').update_attribute(:type, 'Juice')
  end

  def self.down
    remove_column :items, 'type'
  end
end

I actually have to rollback the migration, then run it again (twice in total) for the values to appear. What is going on?


Try adding

Items.reset_column_information

after your add_column statement. ActiveRecord caches the column info, so you need to reload before using them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜