Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have you tried overwriting create/save and their destructive versions? ActiveRecord::Base.create, ActiveRecord::Base.save and their destructive versions are wrapped in a transaction, they're also what trigger callbacks and validations. If you're overriding it, only the stuff done by super will be part of a transaction. If you need yo run validations before then you can explicitly call valid to run them all.</p> <p>Example:</p> <pre><code>before_create :before_create_actions_that_can_be_rolled_back def create if valid? &amp;&amp; before_create_actions_that_wont_be_rolled_back super end end def before_create_actions_that_wont_be_rolled_back # exactly what it sounds like end def before_create_actions_that_can_be_rolled_back # exactly what it sounds like end </code></pre> <p>Caveat: With these modifications the methods will be called in this order:</p> <ol start="2"> <li>before validation (on_create)</li> <li>validate</li> <li>after validation (on_create)</li> <li>before_create_actions_that_wont_be_rolled_back</li> <li>before validation (on_create)</li> <li>validate</li> <li>after validation (on_create)</li> <li>before save callbacks</li> <li>before create callbacks</li> <li>record is created</li> <li>after create callbacks</li> <li>after save callbacks</li> </ol> <p>If any validations fail or if any callback returns false in steps 5-12 the database will be rolled back to the state it was in before step 5. </p> <p>If valid? fails, or before_create_actions_that_wont_be_rolled_back fails than the whole chain will be halted.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload