LoginSignup
5
4

More than 5 years have passed since last update.

Activerecourdでセレクトしたレコードを一括更新

Last updated at Posted at 2014-08-21

update_all

    Book.where('member_id = ?', 333).update_all(:sync_flag => true)

Bookテーブルのmember_idが333のsync_flagをtrueに変更する。

ただし、update_allはActiveRecordオブジェクトを介さないため、validationや、コールバックを通らない。

SQLのupdate文に近い

transactionが必要な処理で使うケースはすくない。

しかし、速度が遅くなることや、更新が失敗することを心配するひつようなない。

適宜使い分けが必要となる

5
4
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
5
4