0

We set few values on a certain row during migration, how do we handle this on a rollback. I would like to set empty null values on down

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
Sam
  • 8,387
  • 19
  • 62
  • 97

2 Answers2

1

You can do rake db:rollback STEP=2.

You can substitute 2 for however many migrations you want to go back.

You can also try this:

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
sjain
  • 23,126
  • 28
  • 107
  • 185
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
Sachin R
  • 11,606
  • 10
  • 35
  • 40