Living on the edge (of Rails) #19 – change_table for migrations and more

In: Edge Rails|Open Source|Ruby|Ruby on Rails

4 May 2008

This week’s report covers changes from 29 April 2008 to 4th May 2008 (the day the corresponding Rails Envy podcast was recorded).

change_table for ActiveRecord migrations

Thanks to Jeff Dean, who also blogged about the new change_table feature in ActiveRecord migrations, you can now change a table with a block like so:

change_table :videos do |t|
  t.add_timestamps
  t.add_belongs_to :goat
  t.add_string :name, :email, :limit => 20
  t.remove_column :name, :email # takes multiple arguments
  t.rename :new_name
  t.string :new_string_column # executes against the renamed table name
end

Some key things to note:

  • add_XXX would add a new column for you, e.g. add_string would add a new string field.
  • Of course, add_timestamps would add the magic created_at and updated_at datetime fields.
  • remove_column now takes multiple arguments.
  • rename would rename the table.

Very nice, DRY enhancement, props to Jeff Dean once again.

Related changeset: http://github.com/rails/rails/commit/96980bd561d79824b6cb6efbcbecdcbf8785d452

ActiveRecord::Base.create takes a block like ActiveRecord::Base.new

Yup now you can also create ActiveRecord objects with a block argument just like you could for ActiveRecord::Base.new:

@person = Person.create(params[:person]) do |p|
  p.name = 'Konata Izumi'
  p.age = 17
end

Credit goes to Adam Meehan for this patch.

Related changeset: http://github.com/rails/rails/commit/dd120ede53eaf71dee76894998a81626b7a689fc

Bugfix: change_column should be able to use :null => true on a field that
formerly had false

You can now use change_column in your migrations to alter a column as nullable if it was previously NOT NULL.

This bugfix is courtesy of Nate Wiger.

Related changeset: http://github.com/rails/rails/commit/10ef65a3b054270ed3d458ec8eb7c2b9a3e638f7

As always, let me know of any suggestions or how I can improve the Living on the Edge (of Rails) series.

7 Responses to Living on the edge (of Rails) #19 – change_table for migrations and more

Avatar

Ben

May 5th, 2008 at 1am

Category should be Ruby on Rails, no?

Avatar

Chu Yeow

May 5th, 2008 at 9am

You’re right, Ben. Fixed!

Avatar

Duck Punching » Passing a block to ActiveRecord create

May 8th, 2008 at 10am

[…] you to pass a block to the AR create class method. It got a mention on the RailsEnvy podcast in the Living on the Edge […]

Avatar

Nome do Jogo » Artigo » Rails Podcast Brasil - Epis?dio 16

May 10th, 2008 at 12am

[…] Living on the edge (of Rails) #19 ? change_table for migrations and more […]

Avatar

Ryan Bates

May 15th, 2008 at 9am

The methods mentioned for change_table are incorrect. You can see this pastie for the correct usage.

http://pastie.caboo.se/197301

Avatar

Double Shot #200 | Developer Home

May 24th, 2008 at 2am

[…] Living on the Edge (of Rails) #19 – The latest changes. With 2.1 about to release, things are getting nailed down, it looks like. […]

Avatar

Space Babies » Blog Archive » Things I love the bestest about Rails 2.1

June 11th, 2008 at 4pm

[…] change_table migration looks a heckuva lot more solid than individual column changes. […]