Can you create a migration to modify data with the where clause in Rails 3

I have several tables loaded with data, where many records have a status field of 0. I want to change them to 1. Is it possible to write a migration like this?

class UpdateStatusContent < ActiveRecord::Migration
  def self.up
    MiscDescription.where ["status = ?", 0].update ["status = ?", 1]
    QuestionsBasic.where ["status = ?", 0].update ["status = ?", 1]
    QuestionsStrength.where ["status = ?", 0].update ["status = ?", 1]
  end

  def self.down
  end
end

I could do this directly in MySQL, but would prefer to use porting. I searched and experimented a bit and couldn't find a solution that works.

Thank you for your help.

+3
source share
1 answer

Yes, it should be possible. Only update_allinstead update:

MiscDescription.where("status = 0").update_all("status = 1")

(There is no need to use this syntax: ["status = ?", 0]when user input is not involved)

+2
source

All Articles