class AddPartsToOrders < ActiveRecord::Migration def change add_column :orders, :nbr_parts, :integer Order.all.each do |order| if !order.nbr_parts? order.nbr_parts = order.credit_product.parts order.save(:validate => false) end end end end