-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-46394: CRUD remaining sections #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -677,6 +677,156 @@ | |||||||||||||||
``join_context: false`` option on an ``atomically`` block to run | ||||||||||||||||
operations at the end of the block for that block only. | ||||||||||||||||
|
||||||||||||||||
Dirty Tracking | ||||||||||||||||
-------------- | ||||||||||||||||
|
||||||||||||||||
You can track changed ("dirty") fields by using a {+odm+} API similar to | ||||||||||||||||
the one available in Active Model. If you modify a defined field in a | ||||||||||||||||
model, {+odm+} marks the model as dirty and allows you to perform | ||||||||||||||||
special actions. The following sections describe how you can interact | ||||||||||||||||
with dirty models. | ||||||||||||||||
|
||||||||||||||||
View Changes | ||||||||||||||||
~~~~~~~~~~~~ | ||||||||||||||||
|
||||||||||||||||
{+odm+} records changes from the time a model is instantiated, either as | ||||||||||||||||
a new document or by retrieving one from the database, until the time it is | ||||||||||||||||
saved. Any persistence operation clears the changes. | ||||||||||||||||
|
||||||||||||||||
{+odm+} creates model-specific methods that allow you to explore the | ||||||||||||||||
changes to a model instance. The following code demonstrates ways that | ||||||||||||||||
you can view changes on your model instance: | ||||||||||||||||
|
||||||||||||||||
.. literalinclude:: /includes/interact-data/crud.rb | ||||||||||||||||
:language: ruby | ||||||||||||||||
:start-after: start-dirty-tracking-view | ||||||||||||||||
:end-before: end-dirty-tracking-view | ||||||||||||||||
|
||||||||||||||||
.. note:: Tracking Changes to Associations | ||||||||||||||||
|
||||||||||||||||
Setting the associations on a document does not modify the | ||||||||||||||||
``changes`` or ``changed_attributes`` hashes. This is true for all | ||||||||||||||||
types of associations. However, changing the | ||||||||||||||||
``_id`` field on referenced associations causes the changes to | ||||||||||||||||
show up in the ``changes`` and the ``changed_attributes`` hashes. | ||||||||||||||||
|
||||||||||||||||
Reset Changes | ||||||||||||||||
~~~~~~~~~~~~~ | ||||||||||||||||
|
||||||||||||||||
You can reset a changed field to its previous value by calling the | ||||||||||||||||
``reset`` method, as shown in the following code: | ||||||||||||||||
|
||||||||||||||||
.. literalinclude:: /includes/interact-data/crud.rb | ||||||||||||||||
:language: ruby | ||||||||||||||||
:start-after: start-dirty-tracking-reset | ||||||||||||||||
:end-before: end-dirty-tracking-reset | ||||||||||||||||
|
||||||||||||||||
Persistence | ||||||||||||||||
~~~~~~~~~~~ | ||||||||||||||||
|
||||||||||||||||
{+odm+} uses dirty tracking as the basis of all persistence operations. | ||||||||||||||||
It evaluates the changes on a document and atomically updates only what | ||||||||||||||||
has changed, compared to other frameworks that write the entire document on | ||||||||||||||||
each save. If you don't make any changes, {+odm+} does not access the | ||||||||||||||||
database when you call ``Model#save``. | ||||||||||||||||
|
||||||||||||||||
View Previous Changes | ||||||||||||||||
~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||
|
||||||||||||||||
After you persist a model to MongoDB, you can still see what changes | ||||||||||||||||
were made previously by calling the ``Model#previous_changes`` method, | ||||||||||||||||
as shown in the following code: | ||||||||||||||||
|
||||||||||||||||
.. literalinclude:: /includes/interact-data/crud.rb | ||||||||||||||||
:language: ruby | ||||||||||||||||
:start-after: start-dirty-tracking-prev | ||||||||||||||||
:end-before: end-dirty-tracking-prev | ||||||||||||||||
|
||||||||||||||||
Update Container Fields | ||||||||||||||||
----------------------- | ||||||||||||||||
|
||||||||||||||||
{+odm+} currently has an issue that prevents changes to attributes of | ||||||||||||||||
Check failure on line 748 in source/interact-data/crud.txt
|
||||||||||||||||
container types, such as ``Set`` or ``Array``, from saving to MongoDB. | ||||||||||||||||
You must assign all fields, including container types, for their values | ||||||||||||||||
to save to MongoDB. | ||||||||||||||||
Comment on lines
+746
to
+752
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should add some kind of "Limitations" page for things like this? It seems weird to have an issue like this nested so far in a page. No action needed on this ticket but just wanted to raise the question. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! That could be a backlog ticket that we could file outside of the standardization. i can make it now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||
|
||||||||||||||||
For example, adding an item to a ``Set`` instance as shown in the | ||||||||||||||||
following code *does not* persist changes to MongoDB: | ||||||||||||||||
|
||||||||||||||||
.. code-block:: ruby | ||||||||||||||||
|
||||||||||||||||
person = Person.new | ||||||||||||||||
person.interests | ||||||||||||||||
# => #<Set: {}> | ||||||||||||||||
|
||||||||||||||||
person.interests << 'Hiking' | ||||||||||||||||
# => #<Set: {"Hiking"}> | ||||||||||||||||
person.interests | ||||||||||||||||
# => #<Set: {}> # Change does not take effect | ||||||||||||||||
|
||||||||||||||||
To persist this change, you must modify the field value *outside* of the | ||||||||||||||||
model and assign it back to the model as shown in the following code: | ||||||||||||||||
|
||||||||||||||||
.. literalinclude:: /includes/interact-data/crud.rb | ||||||||||||||||
:language: ruby | ||||||||||||||||
:start-after: start-container-save | ||||||||||||||||
:end-before: end-container-save | ||||||||||||||||
|
||||||||||||||||
Read-only Documents | ||||||||||||||||
------------------- | ||||||||||||||||
|
||||||||||||||||
You can mark documents as read-only in the following ways, depending on | ||||||||||||||||
the value of the ``Mongoid.legacy_readonly`` feature flag: | ||||||||||||||||
|
||||||||||||||||
- If this flag is turned *off*, you can mark a document as | ||||||||||||||||
read-only by calling the ``readonly!`` method on that document. The | ||||||||||||||||
resulting read-only document raises a ``ReadonlyDocument`` error if | ||||||||||||||||
you attempt to perform any persistence operation, including, but not | ||||||||||||||||
limited to, saving, updating, deleting, and destroying. Note that | ||||||||||||||||
reloading *does not* reset the read-only state. | ||||||||||||||||
|
||||||||||||||||
.. code-block:: ruby | ||||||||||||||||
|
||||||||||||||||
person = Person.first | ||||||||||||||||
person.readonly? # => false | ||||||||||||||||
person.readonly! # Sets the document as read-only | ||||||||||||||||
person.readonly? # => true | ||||||||||||||||
person.name = "Larissa Shay" # Changes the document | ||||||||||||||||
person.save # => raises ReadonlyDocument error | ||||||||||||||||
person.reload.readonly? # => true | ||||||||||||||||
|
||||||||||||||||
- If this flag is turned ``on``, you can mark a document as read-only | ||||||||||||||||
when that document is projected, such as by using ``only`` or | ||||||||||||||||
|
when that document is projected, such as by using ``only`` or | |
when that document is projected, by using methods such as, ``only`` or |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
``without``. The resulting read-only document is not deletable or | |
destroyable, as {+odm+} raises a ``ReadonlyDocument`` error, but it is | |
saveable and updatable. The read-only status *is reset* if you reload the | |
``without``. As a result, you can't delete or destroy the read-only document, | |
as {+odm+} raises a ``ReadonlyDocument`` error, but you can | |
save and update it. The read-only status *is reset* if you reload the |
Check failure on line 809 in source/interact-data/crud.txt
GitHub Actions / TDBX Vale rules
[vale] reported by reviewdog 🐶
[MongoDB.NegativeWords] Use 'remove, delete' instead of the negative word 'destroy'.
Raw Output:
{"message": "[MongoDB.NegativeWords] Use 'remove, delete' instead of the negative word 'destroy'.", "location": {"path": "source/interact-data/crud.txt", "range": {"start": {"line": 809, "column": 13}}}, "severity": "ERROR"}
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be indented to go under the second bullet?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the previous section says: "You can make documents readonly in the following ways..." I think this can be a part of that section instead of having a dedicated header
Override readonly? | |
~~~~~~~~~~~~~~~~~~ | |
You can also make a document read-only by overriding the ``readonly?`` | |
method, as shown in the following code: | |
You can also make a document read-only by overriding the ``readonly?`` | |
method, as shown in the following code: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: clarify that mongoid clears changes after saving to highlight the usefulness of this