2010-10-14 69 views
0

我正在潜入RoR,我需要删除模型及其表格,并更新引用它的其他模型。我在Google上搜索过,所以我发现的最佳答案是this,但答案并不明确。最终的共识是使用ruby script/destroy model方法,然后使用“手动编辑任何可能包含这些已删除模型参考的迁移”这是我不清楚的最后一部分。我想删除的模型我userprofile模式和表...如何在Ruby on Rails中删除模型及其表格?

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 

和更新article模型和表引用他们...

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 

可我只是做ruby script/destroy user和然后在article迁移中调用self.down方法?如果是这样,我该如何称呼'self.down`方法和顺序?

非常感谢您的帮助!

回答

1

是的。只是

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

删除它,然后回滚数据库,或self.down本:

rake db:rollback 

现在可以安全地删除您的迁移文件。