Question: I need to update a table data in database using RAILS migrations. 

```
Sample:
Table: Table_A(Col_A(number), Col_B(varchar),...)
Query: UPDATE Table_A SET Col_B = "XXX" where Col_B = "YYY"

```

What would be the best way to do this using RAILS Migrations. I am not even sure if RAILS migrations is the way to go for updating data in database. Any explanation would be helpful.

Answer: 
I prefer to do any database data changes in a rake task so that's it's

1. Obvious
2. Repeatable
3. Won't later be executed via `rake db:migrate`

The code:

```
namespace :update do
  desc "Update table A to set Col_B to YYY"
    task :table_a => :environment do
      TableA.where(Col_B: "YYY").update_all(Col_B: "XXX")
    end
  end
end

```

Then you can `rake update:table_a` to execute the update.
This should be done in a rake task...

```
namespace :onetime do
  task :update_my_data => :environment do
    TableA.where(Col_B: "YYY").update_all(Col_B: "XXX")
  end
end

```

Then after you deploy:

`rake onetime:update_my_data`

At my company we delete the contents of the onetime namespace rake task after it's been run in production. Just a convention for us I guess.

More details about the `update_all` method: <http://apidock.com/rails/ActiveRecord/Relation/update_all>