Migrating DATA - not just schema, Rails

后端 未结 3 1687
忘掉有多难
忘掉有多难 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 08:02

    Best practice is: don't use models in migrations. Migrations change the way AR maps, so do not use them at all. Do it all with SQL. This way it will always work.

    This:

    User.all.each do |user|
      user.applied_at = user.partner_application_at
      user.save
    end
    

    I would do like this

    update "UPDATE users SET applied_at=partner_application_at"
    

提交回复
热议问题