Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jakecraige/7815478 to your computer and use it in GitHub Desktop.
Save jakecraige/7815478 to your computer and use it in GitHub Desktop.
class ChangeAdditionalInformationColumnsToPromos < ActiveRecord::Migration
class RealPromo < ActiveRecord::Base; self.table_name = 'promos'; end
def up
rename_column :additional_informations, :body, :description
add_column :additional_informations, :title, :string
add_column :additional_informations, :url, :string
add_column :additional_informations, :phone, :string
add_column :additional_informations, :type, :string, default: 'Other'
add_reference :additional_informations, :event, index: true
AdditionalInformation.reset_column_information
RealPromo.all.each do |promo|
hash = promo_hash(promo)
hash['type'] = 'Promo'
AdditionalInformation.create hash
end
end
def down
AdditionalInformation.where({type: 'Promo'}).each do |promo|
RealPromo.create promo_hash(promo)
end
rename_column :additional_informations, :description, :body
remove_column :additional_informations, :title
remove_column :additional_informations, :url
remove_column :additional_informations, :phone
remove_column :additional_informations, :type
remove_reference :additional_informations, :event, index: true
end
private
def promo_hash(promo)
{
title: promo.title,
description: promo.description,
url: promo.url,
phone: promo.phone,
activity_id: promo.activity_id,
event_id: promo.event_id
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment