New columns not immediately available in migrations
Code, Ruby on Rails July 4th, 2007Sometimes you add a column to a table in a migration and then you want populate the new column with some data. Run your migration and while your column has been created in the database, your data does not populate. The problem is that those columns are not accessible via ActiveRecord and so you just need to tell it to update itself:
add_column :user, :favorite_beer, :string User.reset_column_information #<<<<<<<< Here is the ActiveRecord reload tony = User.find_by_name "Tony Spencer" tony.favorite_beer = "Terrapin Rye Pale Ale" tony.save








July 4th, 2007 at 11:48 pm
Thanks,
this is really much nicer than ActiveRecord::Base.connection.execute “SQL goes here” workaround.
August 24th, 2007 at 9:37 pm
I’ve been there time ago. I was going to give up Rails
September 20th, 2007 at 8:21 pm
How did you figure this out? I had the same problem and didnt know where to look…
November 28th, 2007 at 4:48 pm
Bless you, this really made my night. Thanks for posting this tip.
Greg