I have several tables loaded with data where many of the records have the field "status" set to 0. I want to change them to the value 1. Is it possible to write a migration something 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 it directly in MySQL but would prefer to use a migration. I've searched and experimented a bit and have not been able to find a solution that works.
Thanks for your help.
Yes, that should be possible. Only you should use update_all instead of update:
MiscDescription.where("status = 0").update_all("status = 1")
(No need to use this syntax: ["status = ?", 0] when there is no user input involved)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With