|
| 1 | +.. _mongoid-modeling-callbacks: |
| 2 | + |
| 3 | +========= |
| 4 | +Callbacks |
| 5 | +========= |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: ruby framework, odm, code example, life cycle |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to implement **callbacks** in your |
| 24 | +{+odm+} models to customize the life cycle of your model instances. |
| 25 | + |
| 26 | +Callbacks are methods that {+odm+} triggers at specified moments of |
| 27 | +an object's life cycle. They allow you to initiate specified actions |
| 28 | +before or after changes to an object's state. |
| 29 | + |
| 30 | +{+odm+} implements many of the callbacks from Active Record. To learn |
| 31 | +more, see `Callbacks |
| 32 | +<{+active-record-docs+}/active_record_callbacks.html>`__ in the |
| 33 | +Active Record documentation. |
| 34 | + |
| 35 | +Supported Callbacks |
| 36 | +------------------- |
| 37 | + |
| 38 | +Mongoid supports the following callbacks on model classes that implement |
| 39 | +the :ref:`Document <mongoid-modeling-documents>` module: |
| 40 | + |
| 41 | +- ``after_initialize`` |
| 42 | +- ``after_build`` |
| 43 | +- ``before_validation`` |
| 44 | +- ``after_validation`` |
| 45 | +- ``before_create`` |
| 46 | +- ``around_create`` |
| 47 | +- ``after_create`` |
| 48 | +- ``after_find`` |
| 49 | +- ``before_update`` |
| 50 | +- ``around_update`` |
| 51 | +- ``after_update`` |
| 52 | +- ``before_upsert`` |
| 53 | +- ``around_upsert`` |
| 54 | +- ``after_upsert`` |
| 55 | +- ``before_save`` |
| 56 | +- ``around_save`` |
| 57 | +- ``after_save`` |
| 58 | +- ``before_destroy`` |
| 59 | +- ``around_destroy`` |
| 60 | +- ``after_destroy`` |
| 61 | + |
| 62 | +To learn more about any of the preceding callback types, see the |
| 63 | +`ActiveRecord::Callbacks |
| 64 | +<https://api.rubyonrails.org/{+rails-8-version-docs+}/classes/ActiveRecord/Callbacks.html>`__ |
| 65 | +reference in the Rails API documentation. |
| 66 | + |
| 67 | +You can implement callbacks in both top-level and embedded document |
| 68 | +models. |
| 69 | + |
| 70 | +.. note:: Callback Invocation Behavior |
| 71 | + |
| 72 | + For efficiency, {+odm+} invokes the callback only on the document |
| 73 | + that you performed the persistence action on. This behavior enables |
| 74 | + {+odm+} to support large hierarchies and handle optimized atomic |
| 75 | + updates efficiently by not invoking callbacks throughout the document |
| 76 | + hierarchy. |
| 77 | + |
| 78 | +Take precautions and ensure testability when implementing callbacks for |
| 79 | +domain logic, because these designs can lead to unexpected errors when |
| 80 | +callbacks in the chain halt execution. We recommend using callbacks for |
| 81 | +cross-cutting concerns outside of your program's core functionality, |
| 82 | +such as queueing up background jobs. |
| 83 | + |
| 84 | +Document Callbacks |
| 85 | +------------------ |
| 86 | + |
| 87 | +You must implement and register callbacks on your model classes. |
| 88 | +You can register a callback by using ordinary methods, blocks and |
| 89 | +``Proc`` objects, or by defining custom callback objects that use |
| 90 | +classes or modules. |
| 91 | + |
| 92 | +This example demonstrates how to register callbacks on the ``Contact`` |
| 93 | +model class in the following ways: |
| 94 | + |
| 95 | +- Includes the ``before_save`` class method, which triggers the |
| 96 | + ``process_phone`` method before a ``Contact`` instance is saved to |
| 97 | + MongoDB. The ``process_phone`` method is defined separately in the class. |
| 98 | + |
| 99 | +- Includes the ``after_destroy`` class method and uses a block to print a |
| 100 | + message when a ``Contact`` instance is deleted. |
| 101 | + |
| 102 | +.. literalinclude:: /includes/data-modeling/callbacks.rb |
| 103 | + :start-after: start-doc-callback |
| 104 | + :end-before: end-doc-callback |
| 105 | + :language: ruby |
| 106 | + :emphasize-lines: 8, 11-13, 16-18 |
| 107 | + :dedent: |
| 108 | + |
| 109 | +The following code performs data operations that demonstrate the |
| 110 | +callback actions: |
| 111 | + |
| 112 | +.. literalinclude:: /includes/data-modeling/callbacks.rb |
| 113 | + :start-after: start-doc-ops |
| 114 | + :end-before: end-doc-ops |
| 115 | + :language: ruby |
| 116 | + :dedent: |
| 117 | + |
| 118 | +Because callback functionality comes from Active Support, you can |
| 119 | +alternatively use the ``set_callback`` class method syntax to register |
| 120 | +callbacks. The following code demonstrates how to use this syntax to |
| 121 | +create a callback that stores original values of the ``name`` field in |
| 122 | +the ``aliases`` array: |
| 123 | + |
| 124 | +.. literalinclude:: /includes/data-modeling/callbacks.rb |
| 125 | + :start-after: start-doc-set-syntax |
| 126 | + :end-before: end-doc-set-syntax |
| 127 | + :language: ruby |
| 128 | + :emphasize-lines: 8-12 |
| 129 | + :dedent: |
| 130 | + |
| 131 | +Association Callbacks |
| 132 | +--------------------- |
| 133 | + |
| 134 | +{+odm+} provides the following association callbacks: |
| 135 | + |
| 136 | +- ``after_add`` |
| 137 | +- ``after_remove`` |
| 138 | +- ``before_add`` |
| 139 | +- ``before_remove`` |
| 140 | + |
| 141 | +If you register an association callback on your model class, it is |
| 142 | +invoked whenever you add or remove a document from any of the following |
| 143 | +associations: |
| 144 | + |
| 145 | +- ``embeds_many`` |
| 146 | +- ``has_many`` |
| 147 | +- ``has_and_belongs_to_many`` |
| 148 | + |
| 149 | +Specify association callbacks as options on the respective association. |
| 150 | +You must pass the added or removed document as the parameter to the |
| 151 | +specified callback. |
| 152 | + |
| 153 | +The following code demonstrates how to register an association callback |
| 154 | +on a ``User`` model class that embeds multiple ``SavedArticle`` |
| 155 | +instances to limit the number of embedded documents for a single |
| 156 | +instance: |
| 157 | + |
| 158 | +.. literalinclude:: /includes/data-modeling/callbacks.rb |
| 159 | + :start-after: start-association-callback |
| 160 | + :end-before: end-association-callback |
| 161 | + :language: ruby |
| 162 | + :emphasize-lines: 6, 10-15 |
| 163 | + :dedent: |
| 164 | + |
| 165 | +Additional Information |
| 166 | +---------------------- |
| 167 | + |
| 168 | +To learn how to prevent {+odm+} from running callbacks, see the |
| 169 | +following references in the Active Record documentation: |
| 170 | + |
| 171 | +- `Skipping Callbacks <{+active-record-docs+}/active_record_callbacks.html#skipping-callbacks>`__ |
| 172 | +- `Suppressing Saving <{+active-record-docs+}/active_record_callbacks.html#suppressing-saving>`__ |
| 173 | + |
| 174 | +To learn about how {+odm+} manages callbacks in transactions, see the |
| 175 | +:ref:`mongoid-data-txn` guide. |
| 176 | + |
| 177 | +To learn how to access and change your MongoDB data, see the |
| 178 | +:ref:`mongoid-interact-data` guides. |
0 commit comments