migration error

Hi, there. I got an error when I ran the migration to add a filed to database. In the migration file,

def self.up   change_table :contents do |t|     t.add_string :new_names   end end

def self.down   change_table :contents do |t|     t.remove_column :new_names   end end

The error is:

== AddMoreFieldsToContents: migrating

isn’t it t.add_column :new_names, :stirng


Ants Pants wrote:

isn't it t.add_column :new_names, :stirng


Thanks for reply, Ants Pants. It is from a book, Foundation Rails 2 by Eldon Alameda. Here is the quote from the book:

The most recent version of Rails has added yet another shortcut for us in this process as well with the addition of a change_table method. We can use this method in a block in the same way that we did with the create_table method. It supports a number of new convenience methods within the block as well, such as add_XXX (which allows you to easily add a new column, for example, calling add_string to add a new string field), add_timestamps (which adds the magic created_at and updated_at datetime fields), remove_column (which allows you to remove a column and can accept multiple fields), and rename (which allows you to rename the table). So we could have rewritten our last migration like this: def self.up change_table :comments do |t| t.add_string :name, :email, :website end end def self.down change_table :comments do |t| t.remove_column :name, :email, :website end end Very nice and very DRY.

I need to add 10+ columns to database, and I remember the cleaner way from the book. So I gave it a try. But somehow an error occured.

You can do

t.string :foo, :bar, :baz

But I don't think add_string has ever existed, similarly t.timestamps exists, but not t.add_timestamps. Looks like your book is just wrong