# Migration error

**URL:** <https://www.ruby-forum.com/t/migration-error/186624>\
**Category:** Rails\
**Created:** [April 9, 2010, 10:44pm UTC](https://www.ruby-forum.com/t/migration-error/186624 "2010-04-09T22:44:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 9, 2010, 10:44pm UTC](https://www.ruby-forum.com/t/migration-error/186624/1 "2010-04-09T22:44:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 9, 2010, 10:53pm UTC](https://www.ruby-forum.com/t/migration-error/186624/2 "2010-04-09T22:53:07Z")

</div>

isn’t it t.add\_column :new\_names, :stirng

> **[ActiveRecord::Migration](https://api.rubyonrails.org/classes/ActiveRecord/Migration.html)**
>
> Active Record Migrations Migrations can manage the evolution of a schema used by several physical databases.

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 9, 2010, 11:19pm UTC](https://www.ruby-forum.com/t/migration-error/186624/3 "2010-04-09T23:19:41Z")

</div>

Ants P. wrote:

> isn’t it t.add\_column :new\_names, :stirng
> 
> [ActiveRecord::Migration](http://api.rubyonrails.org/classes/ActiveRecord/Migration.html)

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.

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 9, 2010, 11:34pm UTC](https://www.ruby-forum.com/t/migration-error/186624/4 "2010-04-09T23:34:40Z")

</div>

On Apr 9, 10:19 pm, Ichiro S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
