Hey all i am trying to write a migration for a polymorphic association that i made this morning.

upon creating the new column for the members table called management_type since members already belonged to a management i have the management_id for free. I added the has_many :members, :as => :management to the various management models that have members

This allows me to add members to both AlphaManagement and BravoManagement but all the existing members where only on AlphaManagement as i just added BravoManagemnent. so all the existing members have a null value for management_type. i want to just create a migration that will go through each tuple and then add the AlphaManagement association to the management_type column but only if its null. so that on later migrations i don't change them all to AlphaManagement by accident

i see that i could have done something like this to set the default

create_table :products do |t|
  t.references :attachment, :polymorphic => {:default => 'Photo'}
end

but if i have already created the table how could i set a default value for the association through a migration?

EDIT - just so you can see what i have done

1.migration

class AddingManagementTypeToMembers < ActiveRecord::Migration
  def up
    add_column :members, :management_type, :string
  end

  def down
    remove_column :members, :management_type
  end
end
  1. added belongs_to :management, :polymorphic => true to the members class. note managers directors and executives all inherit from member.

  2. the way managements where run have changed but we want to keep legacy reports so we need both the older management and new management models. so in both i added has_many :members, :as => :management

ran migrations. Now adding members to either Alpha or Bravo managements is as easy as bravo_management.members.new(:name => 'rob')

有帮助吗?

解决方案

I have had the same issue previously. You just want to rollback the specific migration using

rake db:migrate:down VERSION=1234123412341234

As answered here, but note this is not the accepted answer in that particular question even though it probably makes a lot sense.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top