Mary Dear
Mary Dear

Reputation: 185

Rails migrations: Drop table and change references

Help me please create a correct migration for next needs

I have this migration before:

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

Table which should be dropped:

class Hiring::QuestionGroup < ActiveRecord::Base
  belongs_to :company
  has_many :questions, class_name: 'Hiring::Question', dependent: :destroy, foreign_key: 'group_id'

This table should be changed:

class Hiring::Question < ActiveRecord::Base
  belongs_to :group, class_name: 'Hiring::QuestionGroup'

to:

class Hiring::Question < ActiveRecord::Base
  belongs_to :company, class_name: ???

How I can do this with One migration?

Upvotes: 1

Views: 3690

Answers (1)

Mary Dear
Mary Dear

Reputation: 185

That's very simple!

def change
    remove_reference :hiring_questions, :group    
    drop_table :hiring_question_groups
    add_reference :hiring_questions, :company
end

Sorry!)

Upvotes: 3

Related Questions