New columns not immediately available in migrations
Sometimes 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
Categories: Code, Ruby on Rails
Thanks,
this is really much nicer than ActiveRecord::Base.connection.execute “SQL goes here” workaround.
I’ve been there time ago. I was going to give up Rails
How did you figure this out? I had the same problem and didnt know where to look…
Bless you, this really made my night. Thanks for posting this tip.
Greg