2010-02-04 44 views
1

我的Ruby on Rails移植一個老既包含了實際的遷移也是一個動作修改數據復位遷移:如何處理失敗的,因爲刪除模型/方法

class AddTypeFlagToGroup < ActiveRecord::Migration 
    def self.up 
    add_column :groups, :selection_type, :string 

    Group.reset_column_information 
    Group.transaction do 
    Group.all.each do |group| 
     group.selection_type = group.calculate_selection_type 
     group.save 
    end 
    end 
    end 

    def self.down 
    remove_column :groups, :selection_type 
    end 
end 

在此遷移有通常的add_columnremove_column遷移語句。但也有一些模型特定的方法調用。

我幾個星期前寫了這個。從那以後,我刪除了我的Group模型,當我使用:reset進行完整遷移時出現錯誤。

rake db:migrate:reset 
(in /Users/jesper/src/pet_project) 

[...] 

== AddTypeFlagToGroup: migrating ============================================= 
-- add_column(:groups, :selection_type, :string) 
    -> 0.0012s 
rake aborted! 
An error has occurred, this and all later migrations canceled: 

uninitialized constant AddTypeFlagToGroup::Group 

問題是,在我的代碼的當前修訂版中,組不存在。我應該如何處理這種「鐵軌方式」?

我在想我可以通過評論Group.xxx這個東西來修改遷移,但是這是一個明智的選擇嗎?

回答

1

現在沒有任何價值可以讓您的遷移中的羣組內容離開您的項目。我只需編輯遷移,從數據庫中刪除所有內容並從頭開始遷移。甚至沒有理由評論它(你正在使用版本控制權嗎?)

另外,我相信遷移的「rails方式」拼寫爲「Arrrrgh!」

+0

+1對於arrrrgh。 – 2010-02-04 14:23:05

+0

修改遷移...這就是我認爲我必須做的事情 – 2010-02-04 15:06:42