Skip to content

Instantly share code, notes, and snippets.

@burke
Created May 1, 2009 00:55
Show Gist options
  • Save burke/104797 to your computer and use it in GitHub Desktop.
Save burke/104797 to your computer and use it in GitHub Desktop.
class ConvertWorkAddressToManyToMany < ActiveRecord::Migration
def self.up
# note that some fields have no "work_" prefix in newfields
oldfields = [:department, :title, :work_street, :work_city, :work_province, :work_postalcode, :work_country, :work_phone, :work_fax, :email]
newfields = [:department, :title, :street, :city, :province, :postalcode, :country, :phone, :fax, :email]
newfields.each do |field|
add_column :contact_organizations, field, String
end
# Copy over all the attributes into the join model.
# I sure as hell hope everyone has a join.
Contact.find(:all).each do |contact|
corg = contact.contact_organizations[0]
# this is decidedly un-ruby-esque, but it'll work fine.
newfields.size.times do |i|
corg[newfields[i]] = contact[oldfields[i]]
end
corg.save!
end
oldfields.each do |field|
remove_column :contacts, field
end
end
def self.down
raise ActiveRecord::IrreversibleMigration
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment