Skip to content

Commit cd29d7f

Browse files
p-mongop
andauthored
MONGOID-5288 Add example overriding _id default with a Proc using pre_processed: true option (#5311)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent ae05909 commit cd29d7f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

source/reference/fields.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,14 +629,23 @@ being operated on:
629629
}
630630

631631
When defining a default value as a ``Proc``, Mongoid will apply the default
632-
after all other attributes are set. To have the default be applied before
633-
the other attributes are set, use the ``pre_processed: true`` field option:
632+
after all other attributes are set and associations are initialized.
633+
To have the default be applied before the other attributes are set,
634+
use the ``pre_processed: true`` field option:
634635

635636
.. code-block:: ruby
636637

637638
field :fulfill_by, type: Time, default: ->{ Time.now + 3.days },
638639
pre_processed: true
639640

641+
The ``pre_processed: true`` option is also necessary when specifying a custom
642+
default value via a ``Proc`` for the ``_id`` field, to ensure the ``_id``
643+
is set correctly via associations:
644+
645+
.. code-block:: ruby
646+
647+
field :_id, type: String, default: -> { 'hello' }, pre_processed: true
648+
640649

641650
.. _storage-field-names:
642651

0 commit comments

Comments
 (0)