class MakeProjectMembershipPolymorphicOnSubject < ActiveRecord::Migration
  def self.up
    add_column :project_memberships, :content_type, :string
    execute "update project_memberships set content_type='Project'"
    rename_table :project_memberships, :content_memberships
    rename_column :content_memberships, :project_id, :content_id
  end

  # Destructive - removes any non-project memberships
  def self.down
    rename_table :content_memberships, :project_memberships

    execute "delete from project_memberships where content_type='Project'"
    remove_column :project_memberships, :content_type
    rename_column :project_memberships, :content_id, :project_id
  end
end

class ProjectMembership < ActiveRecord::Base
  belongs_to :project
  belongs_to :member, :polymorphic => true
end

class MakeProjectMembershipPolymorphicOnSubject < ActiveRecord::Migration
  def self.up
    add_column :project_memberships, :content_type, :string
    ProjectMembership.all.each do |pm|
      pm.content_type = "Project"
      pm.save!
    end
    rename_table :project_memberships, :content_memberships
    rename_column :content_memberships, :project_id, :content_id
  end

  # Destructive - removes any non-project memberships
  def self.down
    rename_table :content_memberships, :project_memberships

    ProjectMembership.all.each do |pm|
      pm.delete if pm.content_type != "Project"
    end

    remove_column :project_memberships, :content_type
    rename_column :project_memberships, :content_id, :project_id
  end
end

--- 
+++ 
@@ -1,15 +1,7 @@
-class ProjectMembership < ActiveRecord::Base
-  belongs_to :project
-  belongs_to :member, :polymorphic => true
-end
-
 class MakeProjectMembershipPolymorphicOnSubject < ActiveRecord::Migration
   def self.up
     add_column :project_memberships, :content_type, :string
-    ProjectMembership.all.each do |pm|
-      pm.content_type = "Project"
-      pm.save!
-    end
+    execute "update project_memberships set content_type='Project'"
     rename_table :project_memberships, :content_memberships
     rename_column :content_memberships, :project_id, :content_id
   end
@@ -18,10 +10,7 @@
   def self.down
     rename_table :content_memberships, :project_memberships
 
-    ProjectMembership.all.each do |pm|
-      pm.delete if pm.content_type != "Project"
-    end
-
+    execute "delete from project_memberships where content_type='Project'"
     remove_column :project_memberships, :content_type
     rename_column :project_memberships, :content_id, :project_id
   end
Resolve an issue where migrations would be canceled in production Rails.env

Referencing a model inside a migration seems to confuse AR's mapping
of tables and columns. Doing a raw execute resolves this.
