0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RailsのTransactionについてのメモ

Posted at

ActiveRecord::Base.transaction

複数のSQLが発行されるときに、すべてのアクションが終わったときにDBを更新する。

具体的には

以下のようなブロック内に処理を書いていく。

ActiveRecord::Base.transaction do
  # 処理
end

例えばインスタンスメソッド内で

ActiveRecord::Base.transaction do
  users.each do |user|
    user.update!
  end
  self.save!
end

これですべての処理の保存と更新を一括で行うことができる。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?