I want to add a column called "payment_type" into my "orders" table.
Here is the migration that I have so far:
def change
add_column :orders, :payment_type, :string
end
I want that payment_type to hold the value "normal" for all the records that currently are in the DB. However, not for the future records. I want no default value for future records. How can I do this?
As you just want to set values for all existing records, you can use update_all
, which is much faster than looping over all instances of order, as it uses just database statements and doesn't instanciate all the orders:
class Order < ActiveRecord::Base
end
def up
add_column :orders, :payment_type, :string
Order.reset_column_information
Order.update_all(payment_type: 'normal')
end
def down
remove_column :orders, :payment_type
end
update_all
does not call any validations or triggers.
I think the easiest way to do it:
class AddStateToSites < ActiveRecord::Migration[5.1]
def up
add_column :sites, :state, :string, default: :complete # sets default value for existed records
change_column :sites, :state, :string, default: nil # changes default value for next
end
def down
remove_column :sites, :state
end
end
And after that check it in console:
>> Site.last.state
Site Load (0.6ms) SELECT "sites".* FROM "sites" ORDER BY "sites"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> "complete"
>> Site.new.state
=> nil
def change
add_column :orders, :payment_type, :string
Order.all.each do |order|
order.update_attributes(:payment_type => 'normal')
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With