Topic: Adding a :default => false to boolean

Does anyone know the syntax for adding the attribute :default => false to an existing migration. In other words lets say I have migration for a Payments table:

class CreatePayments < ActiveRecord::Migration
  def self.up
    create_table :payments do |t|
      t.integer :user_id
      t.float :amount
      t.date :date_of_payment
      t.integer :entered_by
      t.string :ip_address
      t.boolean :archive
      t.timestamps
    end
  end

  def self.down
    drop_table :payments
  end
end

I want to add the new :default => false option that rails introduced recently. How do I do this through a migration? Any help would be appreciated.

"If I'd asked my customers what they wanted, they'd have said a faster horse." - Henry Ford

Re: Adding a :default => false to boolean

As far as I could see at: http://api.rubyonrails.org/classes/Acti … ation.html

Something like

class CreatePayments < ActiveRecord::Migration
  def self.up
    create_table :payments do |t|
      t.integer :user_id, :default => false
      t.float :amount
      t.date :date_of_payment
      t.integer :entered_by
      t.string :ip_address
      t.boolean :archive
      t.timestamps
    end
  end

  def self.down
    drop_table :payments
  end
end

Should do the trick.

Re: Adding a :default => false to boolean

Opps. Sorry. I meant after the migration has been performed.

I am looking for something similar to how rename_column works in a migration but to add the default => false option to an existing boolean column.

"If I'd asked my customers what they wanted, they'd have said a faster horse." - Henry Ford

Re: Adding a :default => false to boolean

That's still just another migration. wink

change_column :payments, :archive, :boolean, :default => false

Re: Adding a :default => false to boolean

There's also the change_column_default shortcut:

 change_column_default(:payments, :archive, false)

sarcasm { I'm sure there's a massive performance in there boost too big_smile }

Rob Anderton
TheWebFellas

Re: Adding a :default => false to boolean

thanks everyone. As always you're an indispensable wealth of information. wink

"If I'd asked my customers what they wanted, they'd have said a faster horse." - Henry Ford