Edit for clarity: I have edited a record in a table in the db. I would like for this record to be consistent across all users who are using the project. All of us are using the same copy of a scrubbed db. I know I could create a migration file for this change but I've seen that migrations are usually used for schema changes of the db. I'm new to rails so please do bear with me.
Asked
Active
Viewed 123 times
-3
-
The question is missing the clarity whether you are checking with Migration or syntax or rails specific details. Please add more context – Rizwan Sep 26 '18 at 03:46
-
To be honest with you, I didn't understand what you're trying to ask and your problem here is. Can you please elaborate a little more ? – Kedarnag Mukanahallipatna Sep 26 '18 at 03:47
-
My question basically is, can i use migration for updating records in a table? in other words to sync the record across the users. – Techguy1611 Sep 26 '18 at 04:50
-
For changing database that does not relate to schema changes, you can write a rake task, then ask the team to run that rake task. Ex: you want to change all `user_uid` from a plain string into a string like `
-user_uid_string` – yeuem1vannam Sep 26 '18 at 06:03 -
@Techguy1611 _"can i use migration for updating records in a table?"_ – yes, you can. From [Migrations and Seed Data](https://edgeguides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data): _"Migrations can also be used to add or modify data."_ – Stefan Sep 26 '18 at 07:33
1 Answers
1
Yes, you can use rails migration for updating database values. Here is a sample of adding new column and updating values for every user:
class AddStatusToUser < ActiveRecord::Migration
def up
add_column :users, :status, :string
User.find_each do |user|
user.status = 'active'
user.save!
end
end
def down
remove_column :users, :status
end
end
In your case you can create new migration and put code to update any table any column value.

Nezir
- 6,727
- 12
- 54
- 78