Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rails 3: How to add a new field to an existing database table
Existing schema is below:
ActiveRecord::Schema.define(:version => 20120130161449) do
create_table "movies", :force => true do |t|
t.string "title"
t.string "rating"
t.text "description"
t.datetime "release_date"
t.datetime "created_at"
t.datetime "updated_at"
end
end
To add new field "director" to this schema, I created Migration by:
rails g migration AddDirectorToMovie director:string
The name "AddDirectorToMovie" means much. "Add" creates "change" method in Migration, and "ToMovie" indicates the table concerned about is "movies."
So, I got a migration file, db/migrate/20120323210944_add_director_to_movie.rb whose contents is:
class AddDirectorToMovie < ActiveRecord::Migration
def change
add_column :movies, :director, :string
end
end
One last thing to do is:
rake db:migrate
Now, I have an updated schema.rb:
ActiveRecord::Schema.define(:version => 20120323210944) do
create_table "movies", :force => true do |t|
t.string "title"
t.string "rating"
t.text "description"
t.datetime "release_date"
t.datetime "created_at"
t.datetime "updated_at"
t.string "director"
end
end
Also, I could confirm the "director" field was added on Rails console:
1.9.2p290 :012 > Movie.columns.map {|c| c.name}
=> ["id", "title", "rating", "description", "release_date", "created_at", "updated_at", "director"]
@zachguo

This comment has been minimized.

Copy link

@zachguo zachguo commented Aug 19, 2013

Could you please tell me why it doesn't work if I add the change method directly into db/migrate/20111119180638_create_movies.rb?

class CreateMovies < ActiveRecord::Migration
  def up
    create_table :movies do |t|
      t.string :title
      t.string :rating
      t.text :description
      t.datetime :release_date
      t.timestamps
    end
  end

  def change
    add_column :movies, :director, :string
  end

  def down
    drop_table :movies
  end
end
@AWizardIsNeverLate

This comment has been minimized.

Copy link

@AWizardIsNeverLate AWizardIsNeverLate commented Dec 28, 2013

Thank you! This worked for me.

@pdhakad

This comment has been minimized.

Copy link

@pdhakad pdhakad commented Aug 27, 2014

It really works ...

@howardpang28

This comment has been minimized.

Copy link

@howardpang28 howardpang28 commented Jan 3, 2015

thanks!

@dianaaparicio

This comment has been minimized.

Copy link

@dianaaparicio dianaaparicio commented Mar 7, 2016

thanks! 👍 😄

@LookOnTheBrightSide

This comment has been minimized.

Copy link

@LookOnTheBrightSide LookOnTheBrightSide commented Mar 13, 2016

thanks!

@SidMasih

This comment has been minimized.

Copy link

@SidMasih SidMasih commented Oct 6, 2016

Thank You!

@rchrdchn

This comment has been minimized.

Copy link

@rchrdchn rchrdchn commented Mar 14, 2018

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.