2013-01-21 18 views
0

我們在遷移期間在某一行上設置了少量值,我們如何在回滾時處理這些值。我想設置空的空值爲空如何恢復在遷移期間在回滾時設置的幾個值

def up 
f = Foo.where(:name => 'some').first 
f.update_attributes(:val1 => 'val1', :val2 => 'val2'); 
end 

def down 
# What should we do here to revert the migration 
end 

回答

1
rake db:migrate:down VERSION=version_number 

def up 
    f = Foo.where(:name => 'some').first 
    f.update_attributes(:val1 => 'val1', :val2 => 'val2') 
end 

def down 
    f = Foo.where(:name => 'some').first 
    f.update_attributes(:val1 => nil, :val2 => nil) if f.present? 
end 
+0

是否f.present?發出數據庫調用 – Sam

+0

不,如果Foo.where(:name =>'some')。first首先返回null,那麼它將跳過update_attribute語句。 –

1

您可以執行rake db:rollback STEP = 2。

您可以用2代替想要返回的許多遷移。

你也可以試試這個:

def up 
    f = Foo.where(:name => 'some').first 
    f.update_attributes(:val1 => 'val1', :val2 => 'val2'); 
    end 

    def down 
    f = Foo.where(:name => 'some').first 
    f.update_attributes(:val1 => nil, :val2 => nil); 
    end