views:

24

answers:

1

Hello. I'm diving into RoR and I need to remove a model and its table, as well as update the other models that reference it. I did a search on google and SO and the best answer I found was this, but the answer is unclear to me. The final consensus was to use the ruby script/destroy model method and then "manually edit any migrations that might contain refs to these deleted models" It's this last part that I'm unclear about. I want to delete the models for my user and profile models and tables...

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
        t.string :email
        t.string :password

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

class CreateProfiles < ActiveRecord::Migration
  def self.up
    create_table :profiles do |t|
        t.string :name
        t.integer :user_id

      t.timestamps
    end
  end

  def self.down
    drop_table :profiles
  end
end

and update the article model and table that references them...

class CreateArticles < ActiveRecord::Migration
  def self.up
    create_table :articles do |t|
        t.string :title
        t.text :body
        t.datetime :published_at
        t.string :image

      t.timestamps
    end
  end

  def self.down
    drop_table :articles
  end
end



class AddUserIdToArticles < ActiveRecord::Migration
  def self.up
    add_column :articles, :user_id, :integer
  end

  def self.down
    remove_column :articles, :user_id
  end
end

Can I just do ruby script/destroy user and then call the self.down methods in the article migrations? If so, how do I call the 'self.down` methods and in what order?

Thanks so much in advance for your help!

+1  A: 

Hey BeachRunnerJoe,

Yep. Just delete it with

ruby script/destroy model user
ruby script/destroy model profile

And then rollback your database, or self.down with this :

rake db:rollback

Now you can safely delete your migration file.

Trip