Skip to content

Commit 5ae6a08

Browse files
p-mongop
andauthored
MONGOID-5205 Document reloading behavior for changed attributes (#5150)
* revise existing reloading docs * reloading behavior when not found does not raise * test reloading without raising on not found * document referenced associations * test referenced associations * use a different model Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 4fc3a3c commit 5ae6a08

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

source/reference/crud.txt

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -549,31 +549,68 @@ Reloading
549549
=========
550550

551551
Use the ``reload`` method to fetch the most recent version of a document from
552-
the database:
552+
the database. Any unsaved modifications to the document's attributes are lost:
553553

554554
.. code-block:: ruby
555555

556-
band = Band.find(..)
556+
band = Band.create!(name: 'foo')
557+
# => #<Band _id: 6206d06de1b8324561f179c9, name: "foo", description: nil, likes: nil>
558+
559+
band.name = 'bar'
560+
band
561+
# => #<Band _id: 6206d06de1b8324561f179c9, name: "bar", description: nil, likes: nil>
562+
557563
band.reload
564+
# => #<Band _id: 6206d06de1b8324561f179c9, name: "foo", description: nil, likes: nil>
565+
566+
When a document is reloaded, all of its embedded associations are also reloaded
567+
in the same query (since embedded documents are stored in the parent document
568+
on the server). If a document has referenced associations, the loaded
569+
associations' are not reloaded but their values are cleared, such that these
570+
associations would be loaded from the database at the next access.
558571

559572
If the model has a :ref:`shard key <shard-keys>` defined, the shard key value
560573
is included in the reloading query.
561574

562-
.. note::
575+
If the database does not contain a matching document, Mongoid normally raises
576+
``Mongoid::Errors::DocumentNotFound``. However, if the configuration option
577+
``raise_not_found_error`` is set to ``false``, and the database does not
578+
contain a matching document, Mongoid replaces the current document with a newly
579+
created document whose attributes are set to default values. Importantly, this
580+
generally causes the ``_id`` of the document to change, as the following
581+
example demonstrates:
563582

564-
``reload`` also works when the document has not been persisted, in which case
565-
it performs a query using the ``id`` value (and shard key value, if a shard
566-
key is defined):
583+
.. code-block:: ruby
567584

568-
.. code-block:: ruby
585+
band = Band.create!
586+
# => #<Band _id: 6206d00de1b8324561f179c7, name: "foo", description: nil, likes: nil>
569587

570-
existing = Band.create!(name: 'Photek')
571-
572-
# Unsaved document
573-
band = Band.new(id: existing.id)
574-
band.reload
575-
band.name
576-
# => "Photek"
588+
Mongoid.raise_not_found_error = false
589+
band.destroy
590+
591+
band.reload
592+
# => #<Band _id: 6206d031e1b8324561f179c8, name: nil, description: nil, likes: nil>
593+
594+
For this reason, it is not recommended to use ``reload`` when
595+
``raise_not_found_error`` is set to ``false``.
596+
597+
598+
Reloading Unsaved Documents
599+
---------------------------
600+
601+
``reload`` can be called when the document has not yet been persisted.
602+
In this case ``reload`` performs a ``find`` query using the ``id`` value
603+
specified in the document (and the shard key value, if a shard key is defined):
604+
605+
.. code-block:: ruby
606+
607+
existing = Band.create!(name: 'Photek')
608+
609+
# Unsaved document
610+
band = Band.new(id: existing.id)
611+
band.reload
612+
band.name
613+
# => "Photek"
577614

578615

579616
Accessing Field Values

0 commit comments

Comments
 (0)