Migrating DATA - not just schema, Rails

后端 未结 3 1701
忘掉有多难
忘掉有多难 2020-12-31 07:16

Sometimes, data migrations are required. As time passes, code changes and migrations using your domain model are no longer valid and migrations fail. What a

3条回答
  •  情话喂你
    2020-12-31 07:52

    This is a perfect example of the Using Models in Your Migrations

    class ChangeFromPartnerAppliedToAppliedAt < ActiveRecord::Migration
      class User < ActiveRecord::Base
      end
    
      def up
        User.all.each do |user|
          user.applied_at = user.partner_application_at
          user.save
       end
     end
    

    Edited after Mischa's comment

    class ChangeFromPartnerAppliedToAppliedAt < ActiveRecord::Migration
      class User < ActiveRecord::Base
      end
    
      def up
        User.update_all('applied_at = partner_application_at')
      end
     end
    

提交回复
热议问题