views:

37

answers:

1

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('NASTY_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?

+6  A: 

Try adding

Items.reset_column_information

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

zetetic
i was just trying to remember this method name!
j.
Well, I had to look it up :)
zetetic