Home > Code, Ruby on Rails > New columns not immediately available in migrations

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"
Categories: Code, Ruby on Rails Tags:
  1. July 4th, 2007 at 23:48 | #1

    this is really much nicer than ActiveRecord::Base.connection.execute “SQL goes here” workaround.

  2. August 24th, 2007 at 21:37 | #2

    I’ve been there time ago. I was going to give up Rails :)

  3. Scott
    September 20th, 2007 at 20:21 | #3

    How did you figure this out? I had the same problem and didnt know where to look… :)

  4. Greg Lappen
    November 28th, 2007 at 16:48 | #4

    Bless you, this really made my night. Thanks for posting this tip.