LoginSignup
17
17

More than 5 years have passed since last update.

update_allはlock_versionもupdated_atも更新しない

Posted at

ActiveRecord の一括更新メソッドである update_all ですが、これを使うと lock_version や updated_at カラムを更新してくれません。

ActiveRecord::Relation#update_all does not update updated_at | github

これはこういう仕様のようです。やるなら自分でやれと。
仕方がないのでこんな風にやりました。

Task.where(:id.in => assign_tasks.map(&:id)).update_all(:status => 'entered')
        
Task.where(:id.in => assign_tasks.map(&:id))
.update_all(["status = 'entered', lock_version = lock_version + 1, updated_at = ? ", Time.now.utc])

実際に運用していると、こういう日付部分って障害や不具合が起きた時の調査の糸口になるので、できるだけちゃんと記録したくなるんですよね。

17
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
17