Krzysztof Witczak
Krzysztof Witczak

Reputation: 512

Rails migration not working

I have strange problem. This is my migration code:

class SetDefaultToFalse < ActiveRecord::Migration
  def change
    MyObject.where('done is ?', nil).each do |a|
      a.done = false
      a.save
    end
  end
end

If I run this on database dump from while ago, with all the other, older migrations, after it's done, any of the my_objects don't have done field marked as false. If I do rake db:rollback and db:migrate again, suddenly, it works. Why? Column done is added more then 5 migrations before this one. Changing previous migrations is not the solution i'm looking for.

Upvotes: 0

Views: 94

Answers (4)

Vrushali Pawar
Vrushali Pawar

Reputation: 3803

I suggest you try this code:

class SetDefaultToFalse < ActiveRecord::Migration
  def self.up
    MyObject.where('done is ?', nil).each do |a|
      a.update_attribute(done: false)
    end
  end
end

Upvotes: 0

TarunJadhwani
TarunJadhwani

Reputation: 1161

You should have a look at this:

Setting different default values in rails migrations?

Upvotes: 1

Airat Sirazutdinov
Airat Sirazutdinov

Reputation: 1

done field added after create. In db schema 'done' field not exist.

Run

rake db:migrate

Upvotes: 0

usmanali
usmanali

Reputation: 2036

I am not sure why would you do that in a migration, but this might help:

MyObject.where('done is ?', nil).update_all(done: false)

Upvotes: 1

Related Questions