Search code examples
ruby-on-railsrubyrails-migrations

How do I maintain a consistent db for a rails project?


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.


Solution

  • 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.