Skip to content

Commit 7dccf12

Browse files
authored
DOCSP-42363: Add association_ids code examples (#41)
* DOCSP-42363: Add association_ids code examples * RR feedback * edit
1 parent 7221a43 commit 7dccf12

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

source/release-notes/mongoid-8.0.txt

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,35 @@ If you call an ``<association>_ids`` method, such as ``member_ids``, on an
832832
association previously loaded into memory, Mongoid 8.0 uses the in-memory collection to satisfy the query
833833
instead of issuing a new query to the database.
834834

835-
To force an ``<association>_ids`` method to issue a new database query,
836-
you can either reset the association or reload the parent record.
835+
For example, the following code calls the ``member_ids`` method and uses the in-memory
836+
collection to retrieve the ``id`` values. The ``id`` of the ``new_member`` is not included
837+
in the output:
837838

839+
.. code-block:: ruby
840+
841+
ids = band.member_ids
842+
# ...
843+
844+
new_member = Member.create(band_id: band.id)
845+
846+
# Prior to 8.0, the method output includes the newly-created member's id.
847+
# Starting in 8.0, the output does not include the newly-created member's id.
848+
p band.member_ids
849+
850+
You can force an ``<association>_ids`` method to issue a new database query
851+
by resetting the association before calling ``<association>_ids``, as shown
852+
in the following code:
853+
854+
.. code-block:: ruby
855+
856+
band.members.reset
857+
858+
# Queries the database rather than the in-memory collection
859+
p band.member_ids
860+
861+
You can also issue a new database query by reloading the parent record
862+
and calling the ``<association>_ids`` method, as shown in the following code:
863+
864+
.. code-block:: ruby
865+
866+
band.reload.member_ids

0 commit comments

Comments
 (0)