首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails迁移:删除表和更改引用

Rails迁移:删除表和更改引用
EN

Stack Overflow用户
提问于 2013-12-05 16:20:52
回答 1查看 3.6K关注 0票数 1

请帮我为下一个需求创建一个正确的迁移

  • 放置表"hiring_question_groups“
  • 将"hiring_questions“中的引用(:组)更改为引用(:company)

我以前也有过这样的迁移:

代码语言:javascript
复制
class CreateHiringQuestionsAndGroups < ActiveRecord::Migration
  def change
    create_table :hiring_questions do |t|
      t.references :group
      t.string :title
      t.text :description
      t.boolean :is_active, default: true
      t.timestamps
    end

    create_table :hiring_question_groups do |t|
      t.references :company
      t.string :title
      t.boolean :is_active, default: true
      t.timestamps
    end
  end
end

应删除的表格:

代码语言:javascript
复制
class Hiring::QuestionGroup < ActiveRecord::Base
  belongs_to :company
  has_many :questions, class_name: 'Hiring::Question', dependent: :destroy, foreign_key: 'group_id'

应更改此表:

代码语言:javascript
复制
class Hiring::Question < ActiveRecord::Base
  belongs_to :group, class_name: 'Hiring::QuestionGroup'

至:

代码语言:javascript
复制
class Hiring::Question < ActiveRecord::Base
  belongs_to :company, class_name: ???

我如何通过一次迁移就可以做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-05 16:54:38

这很简单!

代码语言:javascript
复制
def change
    remove_reference :hiring_questions, :group    
    drop_table :hiring_question_groups
    add_reference :hiring_questions, :company
end

对不起!)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20405196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档