0

这是一个相当常见的重构,Martin Fowler 称之为“移动域”。给定 3 个模型:

class Person < ActiveRecord::Base
  has_one :contact_details
  has_one :address
end

class ContactDetails < ActiveRecord::Base
end

class Address < ActiveRecord::Base
end

如何重构(包括迁移)从 Person 到 ContactDetails 的 has_one 地址?之后模型将如下所示:

class Person < ActiveRecord::Base
  has_one :contact_details
end

class ContactDetails < ActiveRecord::Base
  has_one :address
end

class Address < ActiveRecord::Base
end
4

1 回答 1

0

So I've got as far as the migration, pretty simple actually, just need to rename the foreign key on addresses

class MoveAddressFromPersonToContactDetails < ActiveRecord::Migration

  def self.up
    rename_column :addresses, :person_id, :contact_details_id
  end

  def self.down
    rename_column :addresses, :contact_details_id, :person_id
  end

end

so all that's left is to refactor the code, somehow.

于 2010-09-22T20:00:31.280 回答