Migrations, the "right" way

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
under ActiveRecord::Migration

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

Sexy migrations is syntactic sugar. It is generally preferred.

On May 19, 6:37 pm, “Ryan B. (Radar)” [email protected]

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 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 [email protected] 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
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

what are “sexy migrations”?

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)” [email protected]

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.

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

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

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?