CINXE.COM

ActiveRecord::Transactions::ClassMethods

<!DOCTYPE html> <html lang="en"> <head> <title>ActiveRecord::Transactions::ClassMethods</title> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width,initial-scale=1"> <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" /> <link rel="stylesheet" href="../../../css/panel.css" type="text/css" media="screen" /> <link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" /> <link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" /> <script src="../../../js/jquery-3.5.1.min.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/turbolinks.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/search_index.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/searcher.js" type="text/javascript" charset="utf-8"></script> <script src="../../../panel/tree.js" type="text/javascript" charset="utf-8"></script> <script src="../../../js/searchdoc.js" type="text/javascript" charset="utf-8"></script> <meta name="data-rel-prefix" content="../../../"> <meta name="data-tree-keys" content='["ActiveRecord", "Transactions", "ClassMethods"]'> <meta property="og:title" value="ActiveRecord::Transactions::ClassMethods"> <meta name="description" content="Active Record Transactions Transactions are protective blocks where SQL statements are only permanent if they can all succeed as one atomic action."> <meta property="og:description" content="Active Record Transactions Transactions are protective blocks where SQL statements are only permanent if they can all succeed as one atomic action."> <meta name="keywords" content="ActiveRecord::Transactions::ClassMethods class, transaction, current_transaction, after_commit, after_save_commit, after_create_commit, after_update_commit, after_destroy_commit, after_rollback, set_callback"> </head> <body> <a class="sr-only sr-only-focusable" href="#content" data-turbolinks="false">Skip to Content</a> <a class="sr-only sr-only-focusable" href="#search" data-turbolinks="false">Skip to Search</a> <input type="checkbox" id="hamburger" class="panel_checkbox"> <label class="panel_mobile_button" for="hamburger"><span></span> Menu</label> <nav class="panel panel_tree" id="panel" data-turbolinks-permanent> <div class="header"> <input type="text" placeholder="Search (/) for a class, method, ..." autosave="searchdoc" results="10" id="search" autocomplete="off" tabindex="-1" /> <label class="panel_mobile_button_close" for="hamburger"><span></span> Close</label> </div> <div class="tree"> <ul> </ul> </div> <div class="result"> <ul> </ul> </div> <a href="links.html" id="links">index</a> </nav> <div class="banner"> <span>Ruby on Rails 8.0.0</span><br /> <h2> <span class="type">Module</span> ActiveRecord::Transactions::ClassMethods </h2> <ul class="files"> <li><a href="../../../files/activerecord/lib/active_record/transactions_rb.html">activerecord/lib/active_record/transactions.rb</a></li> </ul> <div id="version-badge">v8.0.0</div> </div> <main id="bodyContent"> <div id="content"> <div class="description"> <h1 id="module-ActiveRecord::Transactions::ClassMethods-label-Active+Record+Transactions">Active Record Transactions</h1> <p>Transactions are protective blocks where SQL statements are only permanent if they can all succeed as one atomic action. The classic example is a transfer between two accounts where you can only have a deposit if the withdrawal succeeded and vice versa. Transactions enforce the integrity of the database and guard the data against program errors or database break-downs. So basically you should use transaction blocks whenever you have a number of statements that must be executed together or not at all.</p> <p>For example:</p> <pre><code>ActiveRecord::Base.transaction do david.withdrawal(100) mary.deposit(100) end </code></pre> <p>This example will only take money from David and give it to Mary if neither <code>withdrawal</code> nor <code>deposit</code> raise an exception. Exceptions will force a ROLLBACK that returns the database to the state before the transaction began. Be aware, though, that the objects will <em>not</em> have their instance data returned to their pre-transactional state.</p> <h2 id="module-ActiveRecord::Transactions::ClassMethods-label-Different+Active+Record+classes+in+a+single+transaction">Different Active Record classes in a single transaction</h2> <p>Though the <a href="ClassMethods.html#method-i-transaction"><code>transaction</code></a> class method is called on some Active Record class, the objects within the transaction block need not all be instances of that class. This is because transactions are per-database connection, not per-model.</p> <p>In this example a <code>balance</code> record is transactionally saved even though <a href="ClassMethods.html#method-i-transaction"><code>transaction</code></a> is called on the <code>Account</code> class:</p> <pre><code>Account.transaction do balance.save! account.save! end </code></pre> <p>The <a href="ClassMethods.html#method-i-transaction"><code>transaction</code></a> method is also available as a model instance method. For example, you can also do this:</p> <pre><code>balance.transaction do balance.save! account.save! end </code></pre> <h2 id="module-ActiveRecord::Transactions::ClassMethods-label-Transactions+are+not+distributed+across+database+connections"><a href="../Transactions.html"><code>Transactions</code></a> are not distributed across database connections</h2> <p>A transaction acts on a single database connection. If you have multiple class-specific databases, the transaction will not protect interaction among them. One workaround is to begin a transaction on each class whose models you alter:</p> <pre><code>Student.transaction do Course.transaction do course.enroll(student) student.units += course.units end end </code></pre> <p>This is a poor solution, but fully distributed transactions are beyond the scope of Active Record.</p> <h2 id="module-ActiveRecord::Transactions::ClassMethods-label-save+and+destroy+are+automatically+wrapped+in+a+transaction"><code>save</code> and <code>destroy</code> are automatically wrapped in a transaction</h2> <p>Both <a href="../Persistence.html#method-i-save">#save</a> and <a href="../Persistence.html#method-i-destroy">#destroy</a> come wrapped in a transaction that ensures that whatever you do in validations or callbacks will happen under its protected cover. So you can use validations to check for values that the transaction depends on or you can raise exceptions in the callbacks to rollback, including <code>after_*</code> callbacks.</p> <p>As a consequence changes to the database are not seen outside your connection until the operation is complete. For example, if you try to update the index of a search engine in <code>after_save</code> the indexer won’t see the updated record. The <a href="ClassMethods.html#method-i-after_commit"><code>after_commit</code></a> callback is the only one that is triggered once the update is committed. See below.</p> <h2 id="module-ActiveRecord::Transactions::ClassMethods-label-Exception+handling+and+rolling+back"><a href="../../Exception.html"><code>Exception</code></a> handling and rolling back</h2> <p>Also have in mind that exceptions thrown within a transaction block will be propagated (after triggering the ROLLBACK), so you should be ready to catch those in your application code.</p> <p>One exception is the <a href="../Rollback.html"><code>ActiveRecord::Rollback</code></a> exception, which will trigger a ROLLBACK when raised, but not be re-raised by the transaction block. Any other exception will be re-raised.</p> <p><strong>Warning</strong>: one should not catch <a href="../StatementInvalid.html"><code>ActiveRecord::StatementInvalid</code></a> exceptions inside a transaction block. <a href="../StatementInvalid.html"><code>ActiveRecord::StatementInvalid</code></a> exceptions indicate that an error occurred at the database level, for example when a unique constraint is violated. On some database systems, such as PostgreSQL, database errors inside a transaction cause the entire transaction to become unusable until it’s restarted from the beginning. Here is an example which demonstrates the problem:</p> <pre><code># Suppose that we have a Number model with a unique column called &#39;i&#39;. Number.transaction do Number.create(i: 0) begin # This will raise a unique constraint error... Number.create(i: 0) rescue ActiveRecord::StatementInvalid # ...which we ignore. end # On PostgreSQL, the transaction is now unusable. The following # statement will cause a PostgreSQL error, even though the unique # constraint is no longer violated: Number.create(i: 1) # =&gt; &quot;PG::Error: ERROR: current transaction is aborted, commands # ignored until end of transaction block&quot; end </code></pre> <p>One should restart the entire transaction if an <a href="../StatementInvalid.html"><code>ActiveRecord::StatementInvalid</code></a> occurred.</p> <h2 id="module-ActiveRecord::Transactions::ClassMethods-label-Nested+transactions">Nested transactions</h2> <p><a href="ClassMethods.html#method-i-transaction"><code>transaction</code></a> calls can be nested. By default, this makes all database statements in the nested transaction block become part of the parent transaction. For example, the following behavior may be surprising:</p> <pre><code>User.transaction do User.create(username: &#39;Kotori&#39;) User.transaction do User.create(username: &#39;Nemu&#39;) raise ActiveRecord::Rollback end end </code></pre> <p>creates both “Kotori” and “Nemu”. Reason is the <a href="../Rollback.html"><code>ActiveRecord::Rollback</code></a> exception in the nested block does not issue a ROLLBACK. Since these exceptions are captured in transaction blocks, the parent block does not see it and the real transaction is committed.</p> <p>In order to get a ROLLBACK for the nested transaction you may ask for a real sub-transaction by passing <code>requires_new: true</code>. If anything goes wrong, the database rolls back to the beginning of the sub-transaction without rolling back the parent transaction. If we add it to the previous example:</p> <pre><code>User.transaction do User.create(username: &#39;Kotori&#39;) User.transaction(requires_new: true) do User.create(username: &#39;Nemu&#39;) raise ActiveRecord::Rollback end end </code></pre> <p>only “Kotori” is created.</p> <p>Most databases don’t support true nested transactions. At the time of writing, the only database that we’re aware of that supports true nested transactions, is MS-SQL. Because of this, Active Record emulates nested transactions by using savepoints. See <a href="https://dev.mysql.com/doc/refman/en/savepoint.html">dev.mysql.com/doc/refman/en/savepoint.html</a> for more information about savepoints.</p> <h3 id="module-ActiveRecord::Transactions::ClassMethods-label-Callbacks">Callbacks</h3> <p>There are two types of callbacks associated with committing and rolling back transactions: <a href="ClassMethods.html#method-i-after_commit"><code>after_commit</code></a> and <a href="ClassMethods.html#method-i-after_rollback"><code>after_rollback</code></a>.</p> <p><a href="ClassMethods.html#method-i-after_commit"><code>after_commit</code></a> callbacks are called on every record saved or destroyed within a transaction immediately after the transaction is committed. <a href="ClassMethods.html#method-i-after_rollback"><code>after_rollback</code></a> callbacks are called on every record saved or destroyed within a transaction immediately after the transaction or savepoint is rolled back.</p> <p>These callbacks are useful for interacting with other systems since you will be guaranteed that the callback is only executed when the database is in a permanent state. For example, <a href="ClassMethods.html#method-i-after_commit"><code>after_commit</code></a> is a good spot to put in a hook to clearing a cache since clearing it from within a transaction could trigger the cache to be regenerated before the database is updated.</p> <h4 id="module-ActiveRecord::Transactions::ClassMethods-label-NOTE-3A+Callbacks+are+deduplicated+per+callback+by+filter.">NOTE: <a href="../Callbacks.html"><code>Callbacks</code></a> are deduplicated per callback by filter.</h4> <p>Trying to define multiple callbacks with the same filter will result in a single callback being run.</p> <p>For example:</p> <pre><code>after_commit :do_something after_commit :do_something # only the last one will be called </code></pre> <p>This applies to all variations of <code>after_*_commit</code> callbacks as well.</p> <pre><code>after_commit :do_something after_create_commit :do_something after_save_commit :do_something </code></pre> <p>It is recommended to use the <code>on:</code> option to specify when the callback should be run.</p> <pre><code>after_commit :do_something, on: [:create, :update] </code></pre> <p>This is equivalent to using <code>after_create_commit</code> and <code>after_update_commit</code>, but will not be deduplicated.</p> <h3 id="module-ActiveRecord::Transactions::ClassMethods-label-Caveats">Caveats</h3> <p>If you’re on MySQL, then do not use Data Definition Language (DDL) operations in nested transactions blocks that are emulated with savepoints. That is, do not execute statements like ‘CREATE TABLE’ inside such blocks. This is because MySQL automatically releases all savepoints upon executing a DDL operation. When <code>transaction</code> is finished and tries to release the savepoint it created earlier, a database error will occur because the savepoint has already been automatically released. The following example demonstrates the problem:</p> <pre><code>Model.lease_connection.transaction do # BEGIN Model.lease_connection.transaction(requires_new: true) do # CREATE SAVEPOINT active_record_1 Model.lease_connection.create_table(...) # active_record_1 now automatically released end # RELEASE SAVEPOINT active_record_1 # ^^^^ BOOM! database error! end </code></pre> <p>Note that “TRUNCATE” is also a MySQL DDL statement!</p> </div> <!-- Method ref --> <div class="sectiontitle">Methods</div> <dl class="methods"> <dt>A</dt> <dd> <ul> <li> <a href="#method-i-after_commit">after_commit</a>, </li> <li> <a href="#method-i-after_create_commit">after_create_commit</a>, </li> <li> <a href="#method-i-after_destroy_commit">after_destroy_commit</a>, </li> <li> <a href="#method-i-after_rollback">after_rollback</a>, </li> <li> <a href="#method-i-after_save_commit">after_save_commit</a>, </li> <li> <a href="#method-i-after_update_commit">after_update_commit</a> </li> </ul> </dd> <dt>C</dt> <dd> <ul> <li> <a href="#method-i-current_transaction">current_transaction</a> </li> </ul> </dd> <dt>S</dt> <dd> <ul> <li> <a href="#method-i-set_callback">set_callback</a> </li> </ul> </dd> <dt>T</dt> <dd> <ul> <li> <a href="#method-i-transaction">transaction</a> </li> </ul> </dd> </dl> <!-- Methods --> <h2 class="sectiontitle">Instance Public methods</h2> <div class="method"> <h3 class="title method-title" id="method-i-after_commit"> <b>after_commit</b>(*args, &amp;block) <a href="../../../classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_commit" name="method-i-after_commit" class="permalink">Link</a> </h3> <div class="description"> <p>This callback is called after a record has been created, updated, or destroyed.</p> <p>You can specify that the callback should only be fired by a certain action with the <code>:on</code> option:</p> <pre><code>after_commit :do_foo, on: :create after_commit :do_bar, on: :update after_commit :do_baz, on: :destroy after_commit :do_foo_bar, on: [:create, :update] after_commit :do_bar_baz, on: [:update, :destroy] </code></pre> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-after_commit_source')" id="l_method-i-after_commit_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activerecord/lib/active_record/transactions.rb#L266" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-after_commit_source" class="dyn-source"> <pre><span class="ruby-comment"># File activerecord/lib/active_record/transactions.rb, line 266</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">after_commit</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">set_options_for_callbacks!</span>(<span class="ruby-identifier">args</span>, <span class="ruby-identifier">prepend_option</span>) <span class="ruby-identifier">set_callback</span>(<span class="ruby-value">:commit</span>, <span class="ruby-value">:after</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-after_create_commit"> <b>after_create_commit</b>(*args, &amp;block) <a href="../../../classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_create_commit" name="method-i-after_create_commit" class="permalink">Link</a> </h3> <div class="description"> <p>Shortcut for <code>after_commit :hook, on: :create</code>.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-after_create_commit_source')" id="l_method-i-after_create_commit_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activerecord/lib/active_record/transactions.rb#L278" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-after_create_commit_source" class="dyn-source"> <pre><span class="ruby-comment"># File activerecord/lib/active_record/transactions.rb, line 278</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">after_create_commit</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">set_options_for_callbacks!</span>(<span class="ruby-identifier">args</span>, <span class="ruby-value">on:</span> <span class="ruby-value">:create</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">prepend_option</span>) <span class="ruby-identifier">set_callback</span>(<span class="ruby-value">:commit</span>, <span class="ruby-value">:after</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-after_destroy_commit"> <b>after_destroy_commit</b>(*args, &amp;block) <a href="../../../classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_destroy_commit" name="method-i-after_destroy_commit" class="permalink">Link</a> </h3> <div class="description"> <p>Shortcut for <code>after_commit :hook, on: :destroy</code>.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-after_destroy_commit_source')" id="l_method-i-after_destroy_commit_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activerecord/lib/active_record/transactions.rb#L290" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-after_destroy_commit_source" class="dyn-source"> <pre><span class="ruby-comment"># File activerecord/lib/active_record/transactions.rb, line 290</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">after_destroy_commit</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">set_options_for_callbacks!</span>(<span class="ruby-identifier">args</span>, <span class="ruby-value">on:</span> <span class="ruby-value">:destroy</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">prepend_option</span>) <span class="ruby-identifier">set_callback</span>(<span class="ruby-value">:commit</span>, <span class="ruby-value">:after</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-after_rollback"> <b>after_rollback</b>(*args, &amp;block) <a href="../../../classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_rollback" name="method-i-after_rollback" class="permalink">Link</a> </h3> <div class="description"> <p>This callback is called after a create, update, or destroy are rolled back.</p> <p>Please check the documentation of <a href="ClassMethods.html#method-i-after_commit"><code>after_commit</code></a> for options.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-after_rollback_source')" id="l_method-i-after_rollback_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activerecord/lib/active_record/transactions.rb#L298" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-after_rollback_source" class="dyn-source"> <pre><span class="ruby-comment"># File activerecord/lib/active_record/transactions.rb, line 298</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">after_rollback</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">set_options_for_callbacks!</span>(<span class="ruby-identifier">args</span>, <span class="ruby-identifier">prepend_option</span>) <span class="ruby-identifier">set_callback</span>(<span class="ruby-value">:rollback</span>, <span class="ruby-value">:after</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-after_save_commit"> <b>after_save_commit</b>(*args, &amp;block) <a href="../../../classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_save_commit" name="method-i-after_save_commit" class="permalink">Link</a> </h3> <div class="description"> <p>Shortcut for <code>after_commit :hook, on: [ :create, :update ]</code>.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-after_save_commit_source')" id="l_method-i-after_save_commit_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activerecord/lib/active_record/transactions.rb#L272" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-after_save_commit_source" class="dyn-source"> <pre><span class="ruby-comment"># File activerecord/lib/active_record/transactions.rb, line 272</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">after_save_commit</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">set_options_for_callbacks!</span>(<span class="ruby-identifier">args</span>, <span class="ruby-value">on:</span> [ <span class="ruby-value">:create</span>, <span class="ruby-value">:update</span> ], <span class="ruby-operator">**</span><span class="ruby-identifier">prepend_option</span>) <span class="ruby-identifier">set_callback</span>(<span class="ruby-value">:commit</span>, <span class="ruby-value">:after</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-after_update_commit"> <b>after_update_commit</b>(*args, &amp;block) <a href="../../../classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_update_commit" name="method-i-after_update_commit" class="permalink">Link</a> </h3> <div class="description"> <p>Shortcut for <code>after_commit :hook, on: :update</code>.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-after_update_commit_source')" id="l_method-i-after_update_commit_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activerecord/lib/active_record/transactions.rb#L284" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-after_update_commit_source" class="dyn-source"> <pre><span class="ruby-comment"># File activerecord/lib/active_record/transactions.rb, line 284</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">after_update_commit</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">set_options_for_callbacks!</span>(<span class="ruby-identifier">args</span>, <span class="ruby-value">on:</span> <span class="ruby-value">:update</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">prepend_option</span>) <span class="ruby-identifier">set_callback</span>(<span class="ruby-value">:commit</span>, <span class="ruby-value">:after</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-current_transaction"> <b>current_transaction</b>() <a href="../../../classes/ActiveRecord/Transactions/ClassMethods.html#method-i-current_transaction" name="method-i-current_transaction" class="permalink">Link</a> </h3> <div class="description"> <p>Returns a representation of the current transaction state, which can be a top level transaction, a savepoint, or the absence of a transaction.</p> <p>An object is always returned, whether or not a transaction is currently active. To check if a transaction was opened, use <code>current_transaction.open?</code>.</p> <p>See the <a href="../Transaction.html"><code>ActiveRecord::Transaction</code></a> documentation for detailed behavior.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-current_transaction_source')" id="l_method-i-current_transaction_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activerecord/lib/active_record/transactions.rb#L245" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-current_transaction_source" class="dyn-source"> <pre><span class="ruby-comment"># File activerecord/lib/active_record/transactions.rb, line 245</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">current_transaction</span> <span class="ruby-identifier">connection_pool</span>.<span class="ruby-identifier">active_connection</span>&amp;.<span class="ruby-identifier">current_transaction</span>&amp;.<span class="ruby-identifier">user_transaction</span> <span class="ruby-operator">||</span> <span class="ruby-constant">Transaction</span><span class="ruby-operator">::</span><span class="ruby-constant">NULL_TRANSACTION</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-set_callback"> <b>set_callback</b>(name, *filter_list, &amp;block) <a href="../../../classes/ActiveRecord/Transactions/ClassMethods.html#method-i-set_callback" name="method-i-set_callback" class="permalink">Link</a> </h3> <div class="description"> <p>Similar to <a href="../../ActiveSupport/Callbacks/ClassMethods.html#method-i-set_callback"><code>ActiveSupport::Callbacks::ClassMethods#set_callback</code></a>, but with support for options available on <a href="ClassMethods.html#method-i-after_commit"><code>after_commit</code></a> and <a href="ClassMethods.html#method-i-after_rollback"><code>after_rollback</code></a> callbacks.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-set_callback_source')" id="l_method-i-set_callback_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activerecord/lib/active_record/transactions.rb#L305" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-set_callback_source" class="dyn-source"> <pre><span class="ruby-comment"># File activerecord/lib/active_record/transactions.rb, line 305</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">set_callback</span>(<span class="ruby-identifier">name</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">filter_list</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">options</span> = <span class="ruby-identifier">filter_list</span>.<span class="ruby-identifier">extract_options!</span> <span class="ruby-identifier">filter_list</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">options</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">name</span>.<span class="ruby-identifier">in?</span>([<span class="ruby-value">:commit</span>, <span class="ruby-value">:rollback</span>]) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:on</span>] <span class="ruby-identifier">fire_on</span> = <span class="ruby-constant">Array</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:on</span>]) <span class="ruby-identifier">assert_valid_transaction_action</span>(<span class="ruby-identifier">fire_on</span>) <span class="ruby-identifier">options</span>[<span class="ruby-value">:if</span>] = [ <span class="ruby-operator">-&gt;</span> { <span class="ruby-identifier">transaction_include_any_action?</span>(<span class="ruby-identifier">fire_on</span>) }, <span class="ruby-operator">*</span><span class="ruby-identifier">options</span>[<span class="ruby-value">:if</span>] ] <span class="ruby-keyword">end</span> <span class="ruby-keyword">super</span>(<span class="ruby-identifier">name</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">filter_list</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div class="method"> <h3 class="title method-title" id="method-i-transaction"> <b>transaction</b>(**options, &amp;block) <a href="../../../classes/ActiveRecord/Transactions/ClassMethods.html#method-i-transaction" name="method-i-transaction" class="permalink">Link</a> </h3> <div class="description"> <p>See the <a href="../ConnectionAdapters/DatabaseStatements.html#method-i-transaction"><code>ConnectionAdapters::DatabaseStatements#transaction</code></a> API docs.</p> </div> <div class="sourcecode"> <p class="source-link"> Source: <a href="javascript:toggleSource('method-i-transaction_source')" id="l_method-i-transaction_source">show</a> | <a href="https://github.com/rails/rails/blob/dd8f7185faeca6ee968a6e9367f6d8601a83b8db/activerecord/lib/active_record/transactions.rb#L232" target="_blank" class="github_url">on GitHub</a> </p> <div id="method-i-transaction_source" class="dyn-source"> <pre><span class="ruby-comment"># File activerecord/lib/active_record/transactions.rb, line 232</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">transaction</span>(<span class="ruby-operator">**</span><span class="ruby-identifier">options</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">with_connection</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">connection</span><span class="ruby-operator">|</span> <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">transaction</span>(<span class="ruby-operator">**</span><span class="ruby-identifier">options</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> </div> </main> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10