【发布时间】:2014-03-13 15:48:33
【问题描述】:
我的应用中有一个 has_many 关系。例如部门有很多用户。 我想将其转换为 has_and_belongs_to_many 关系。 作为迁移的一部分,我需要保留用户和部门之间的当前关系,这意味着我必须将所有数据移动到新的连接表中。 这是我创建的迁移:
class CreateUserDepartment < ActiveRecord::Migration
def change
create_table :users_departments do |t|
t.belongs_to :user
t.belongs_to :department
end
###############################################
# need to move the data to the new table here #
###############################################
remove_column :users, :sub_department_id
end
end
什么是写缺失行的最佳方法?
【问题讨论】:
标签: ruby-on-rails database-migration