如何执行“移动字段”重构活动记录模型
这是一种相当常见的重构,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
This is a fairly common refactoring, Martin Fowler calls it 'move field'. Given 3 models:
class Person < ActiveRecord::Base
has_one :contact_details
has_one :address
end
class ContactDetails < ActiveRecord::Base
end
class Address < ActiveRecord::Base
end
how do I refactor, including migration, the has_one address from Person to ContactDetails? Afterwards the models would look like:
class Person < ActiveRecord::Base
has_one :contact_details
end
class ContactDetails < ActiveRecord::Base
has_one :address
end
class Address < ActiveRecord::Base
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我已经完成了迁移,实际上非常简单,只需要重命名地址上的外键
,所以剩下的就是以某种方式重构代码。
So I've got as far as the migration, pretty simple actually, just need to rename the foreign key on addresses
so all that's left is to refactor the code, somehow.