# Migrations, the "right" way

**URL:** <https://www.ruby-forum.com/t/migrations-the-right-way/138077>\
**Category:** Rails\
**Created:** [May 20, 2008, 1:00am UTC](https://www.ruby-forum.com/t/migrations-the-right-way/138077 "2008-05-20T01:00:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [May 20, 2008, 1:00am UTC](https://www.ruby-forum.com/t/migrations-the-right-way/138077/1 "2008-05-20T01:00:46Z")

</div>

I’ve got the Agile Web D. with Rails book that tells me to do  
migrations different than the what is explained on [api.rubyonrails.org](http://api.rubyonrails.org)  
under ActiveRecord::Migration

What is the “best” or “right” syntax of migrations?

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [May 20, 2008, 4:32am UTC](https://www.ruby-forum.com/t/migrations-the-right-way/138077/2 "2008-05-20T04:32:03Z")

</div>

Sexy migrations is syntactic sugar. It is generally preferred.

On May 19, 6:37 pm, “Ryan B. (Radar)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [May 20, 2008, 1:38am UTC](https://www.ruby-forum.com/t/migrations-the-right-way/138077/3 "2008-05-20T01:38:15Z")

</div>

For those of us who don’t own the book,

How does the book tell you to do it?

ActiveRecord::Migration on the [api.rubyonrails.org](http://api.rubyonrails.org) seems to be the new  
syntax, where AWDWR will probably be the older (but not deprecated)  
syntax.  
On Tue, May 20, 2008 at 8:30 AM, Dustin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve got the Agile Web D. with Rails book that tells me to do  
> migrations different than the what is explained on [api.rubyonrails.org](http://api.rubyonrails.org)  
> under ActiveRecord::Migration
> 
> What is the “best” or “right” syntax of migrations?
> 
> >

–  
Appreciated my help?  
Reccommend me on Working With Rails  
[http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [May 20, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/migrations-the-right-way/138077/4 "2008-05-20T17:21:06Z")

</div>

what are “sexy migrations”?

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [May 20, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/migrations-the-right-way/138077/5 "2008-05-20T17:19:18Z")

</div>

Here is an example from the book:

class CreateProducts \< ActiveRecord::Migration  
def self.up  
create\_table :products do |t|  
t.column :title, :string  
t.column :description, :text  
t.column :image\_url, :string  
end  
end

def self.down  
drop\_table :products  
end  
end

On May 19, 5:37 pm, “Ryan B. (Radar)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [May 20, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/migrations-the-right-way/138077/6 "2008-05-20T18:50:59Z")

</div>

Basically, the rails core realized that the ‘column’ call was  
redundant within the context of a table definition so you can now do  
the following

create\_table :products do |t|  
t.column :title, :string  
t.column :description, :text  
t.column :image\_url, :string  
end

Like this:

create\_table :products do |t|  
t.string :title  
t.text :description  
t.string :image\_url  
end

…or…

create\_table :products do |t|  
t.string :title, :image\_url  
t.text :description  
end

What I personally find “sexier” is the syntax that makes references to  
other tables more explicit. For example, if a line\_item referred to  
the product table you used to do this:

create\_table :line\_items do |t|  
t.column :product\_id, :integer  
end

But now…

create\_table :line\_items do |t|  
t.references :product  
end

And you can have even sexier polymorphic associations

create\_table :addresses do |t|  
t.references :addressable, :polymorphic=\>true  
end

That gives you :addressable\_type and :addressable\_id with a syntax  
that looks remarkably like the one you’ll have in your Address class.

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [May 20, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/migrations-the-right-way/138077/7 "2008-05-20T22:48:19Z")

</div>

nevermind, I found the answer here:  
[http://www.railsforum.com/viewtopic.php?id=17431](http://www.railsforum.com/viewtopic.php?id=17431)

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [May 21, 2008, 1:49am UTC](https://www.ruby-forum.com/t/migrations-the-right-way/138077/8 "2008-05-21T01:49:39Z")

</div>

> what are “sexy migrations”?

Migrations with sexy lace and a single rose clutched between it’s teeth:

Instead of your old, tired cellulite ridden migrations:

def self.up  
create\_table :posts do |t|  
t.column :created\_at, :datetime  
t.column :updated\_at, :datetime  
t.column :title, :string  
t.column :slug, :string  
t.column :text, :text  
t.column :user\_id, :integer  
end  
end

Your younger, fresh, sexy migrations:  
def self.up  
create\_table :posts do |t|  
t.timestamps  
t.string :title, :slug  
t.text :text  
t.references :user  
end  
end

_insert mandatory wolf whistle here_

–  
Appreciated my help?  
Recommend me on Working With Rails  
[http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![dustin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@dustin](https://www.ruby-forum.com/u/dustin)\
**Post date:** [May 20, 2008, 9:36pm UTC](https://www.ruby-forum.com/t/migrations-the-right-way/138077/9 "2008-05-20T21:36:17Z")

</div>

Thanks AndyV

When I try this syntax I get this error:

rake aborted!  
undefined method `integer’ for  
#ActiveRecord::ConnectionAdapters::TableDefinition:0x421d3e8

I just updated rails to 2.0.2, is there something I’m missing?
