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

– change_table(:contents)
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `add_string’ for
#<ActiveRecord::ConnectionAdapters::Table:0x2b
b9b74>

If I used add_column method, everything ran fine. I am running Rails
2.3.4. Any thoughts on the undefined method error?
Thanks in advance.

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

Ants P. wrote:

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

ActiveRecord::Migration

Thanks for reply, Ants P…
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.

On Apr 9, 10:19 pm, Ichiro S. [email protected] wrote:

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

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

Fred