class
Importance_2
Ruby on Rails latest stable (v7.1.3.2) - 0 notes - Superclass: ActiveRecord::ActiveRecordError
  • 1.0.0
  • 1.1.6
  • 1.2.6
  • 2.0.3
  • 2.1.0
  • 2.2.1
  • 2.3.8
  • 3.0.0
  • 3.0.9
  • 3.1.0
  • 3.2.1
  • 3.2.8
  • 3.2.13
  • 4.0.2
  • 4.1.8
  • 4.2.1
  • 4.2.7
  • 4.2.9
  • 5.0.0.1
  • 5.1.7
  • 5.2.3
  • 6.0.0
  • 6.1.3.1
  • 6.1.7.7
  • 7.0.0 (0)
  • 7.1.3.2
  • 7.1.3.4
  • What's this?

Class deprecated or moved

This class is deprecated or moved on the latest stable version. The last existing version (v7.0.0) is shown here.

{ActiveRecord::Base.transaction}[rdoc-ref:Transactions::ClassMethods#transaction] uses this exception to distinguish a deliberate rollback from other exceptional situations. Normally, raising an exception will cause the {.transaction}[rdoc-ref:Transactions::ClassMethods#transaction] method to rollback the database transaction and pass on the exception. But if you raise an ActiveRecord::Rollback exception, then the database transaction will be rolled back, without passing on the exception.

For example, you could do this in your controller to rollback a transaction:

class BooksController < ActionController::Base
  def create
    Book.transaction do
      book = Book.new(params[:book])
      book.save!
      if today_is_friday?
        # The system must fail on Friday so that our support department
        # won't be out of job. We silently rollback this transaction
        # without telling the user.
        raise ActiveRecord::Rollback
      end
    end
    # ActiveRecord::Rollback is the only exception that won't be passed on
    # by ActiveRecord::Base.transaction, so this line will still be reached
    # even on Friday.
    redirect_to root_url
  end
end
Show files where this class is defined (1 file)
Register or log in to add new notes.