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_XXXwould add a new column for you, e.g.
add_stringwould add a new string field.
- Of course, add_timestamps would add the magic
remove_columnnow takes multiple arguments.
renamewould rename the table.
Very nice, DRY enhancement, props to Jeff Dean once again.
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
@person = Person.create(params[:person]) do |p| p.name = 'Konata Izumi' p.age = 17 end
Credit goes to Adam Meehan for this patch.
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
This bugfix is courtesy of Nate Wiger.
As always, let me know of any suggestions or how I can improve the Living on the Edge (of Rails) series.