-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-45330: inheritance #66
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
Merged
rustagir
merged 8 commits into
mongodb:standardized
from
rustagir:DOCSP-45330-inheritance
Nov 18, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9603d85
DOCSP-45330: inheritance (WIP)
rustagir c4c3933
wip
rustagir ff70eaf
vale
rustagir 8fbf2ca
add label
rustagir 81afbe8
fixes
rustagir f006774
fix
rustagir 253cc80
Merge branch 'standardized' of github.com:mongodb/docs-mongoid into D…
rustagir 19d7118
SA PR fixes 1
rustagir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,306 @@ | ||||||||||||||||||||||||||||||||||||||
.. _mongoid-modeling-inheritance: | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
=========== | ||||||||||||||||||||||||||||||||||||||
Inheritance | ||||||||||||||||||||||||||||||||||||||
=========== | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
.. facet:: | ||||||||||||||||||||||||||||||||||||||
:name: genre | ||||||||||||||||||||||||||||||||||||||
:values: reference | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
.. meta:: | ||||||||||||||||||||||||||||||||||||||
:keywords: ruby framework, odm, relationship, code example, polymorphic | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
.. contents:: On this page | ||||||||||||||||||||||||||||||||||||||
:local: | ||||||||||||||||||||||||||||||||||||||
:backlinks: none | ||||||||||||||||||||||||||||||||||||||
:depth: 2 | ||||||||||||||||||||||||||||||||||||||
:class: singlecol | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Overview | ||||||||||||||||||||||||||||||||||||||
-------- | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
In this guide, you can learn how to implement **inheritance** into your | ||||||||||||||||||||||||||||||||||||||
{+odm+} models. Inheritance allows you to apply the characteristics of | ||||||||||||||||||||||||||||||||||||||
one "parent" class to one or more "child" classes. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
{+odm+} supports inheritance in top level and embedded documents. | ||||||||||||||||||||||||||||||||||||||
When a child model class inherits from a parent class, {+odm+} copies | ||||||||||||||||||||||||||||||||||||||
the parent class's fields, associations, validations, and scopes to | ||||||||||||||||||||||||||||||||||||||
the child class. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Assign Inheritance | ||||||||||||||||||||||||||||||||||||||
------------------ | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
When creating a child model class, use the ``<`` character to implement | ||||||||||||||||||||||||||||||||||||||
inheritance from a specified parent class. The following model classes | ||||||||||||||||||||||||||||||||||||||
demonstrate how to create parent and child classes between the | ||||||||||||||||||||||||||||||||||||||
``Person``, ``Employee``, and ``Manager`` models: | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
.. literalinclude:: /includes/data-modeling/inheritance.rb | ||||||||||||||||||||||||||||||||||||||
:start-after: start-simple-inheritance | ||||||||||||||||||||||||||||||||||||||
:end-before: end-simple-inheritance | ||||||||||||||||||||||||||||||||||||||
:language: ruby | ||||||||||||||||||||||||||||||||||||||
:emphasize-lines: 7, 14 | ||||||||||||||||||||||||||||||||||||||
:dedent: | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
When you perform data operations by using the preceding models, | ||||||||||||||||||||||||||||||||||||||
instances of ``Person``, ``Employee``, or ``Manager`` are all saved in the | ||||||||||||||||||||||||||||||||||||||
``people`` collection. {+odm+} sets the ``_type`` discriminator field to | ||||||||||||||||||||||||||||||||||||||
rustagir marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||
the model class name in documents to ensure that documents are returned | ||||||||||||||||||||||||||||||||||||||
as the expected types when you retrieve them. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Embedded Documents | ||||||||||||||||||||||||||||||||||||||
~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
You can also implement an inheritance pattern in embedded associations. | ||||||||||||||||||||||||||||||||||||||
Similar to the behavior of top-level model classes, {+odm+} sets the | ||||||||||||||||||||||||||||||||||||||
``_type`` discriminator field in embedded documents depending on the | ||||||||||||||||||||||||||||||||||||||
model class used to create them. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
The following example adds an embedded association to the ``Person`` | ||||||||||||||||||||||||||||||||||||||
model and creates parent and child models for the embedded ``Info`` | ||||||||||||||||||||||||||||||||||||||
class: | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
.. literalinclude:: /includes/data-modeling/inheritance.rb | ||||||||||||||||||||||||||||||||||||||
:start-after: start-embedded-inheritance | ||||||||||||||||||||||||||||||||||||||
:end-before: end-embedded-inheritance | ||||||||||||||||||||||||||||||||||||||
:language: ruby | ||||||||||||||||||||||||||||||||||||||
:emphasize-lines: 5, 14, 17, 22 | ||||||||||||||||||||||||||||||||||||||
:dedent: | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Query Behavior | ||||||||||||||||||||||||||||||||||||||
~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
When you query on a child model class, the query returns only documents | ||||||||||||||||||||||||||||||||||||||
in which the value of the ``_type`` field match the queried class or | ||||||||||||||||||||||||||||||||||||||
|
in which the value of the ``_type`` field match the queried class or | |
in which the value of the ``_type`` field matches the queried class 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.
[s] Can potentially omit extra words for brevity and make this note easier to follow, for example:
Suggested change
.. note:: | |
Because the discriminator value customization is declared in child classes, | |
you must load the child classes retrieved by a query *before* sending | |
that query. In the preceding example, the ``Employee`` class definition | |
must be loaded before you query on ``Person`` if the returned documents could | |
potentially be instances of ``Employee``. Autoloading isn't able to resolve | |
the discriminator value ``"Worker"`` to return the document as an | |
instance of ``Employee``. | |
.. note:: | |
Because the discriminator value customization is declared in child classes, | |
you must load the child classes retrieved by a query *before* sending | |
that query. In the preceding example, the ``Employee`` class definition | |
must be loaded before you query on ``Person`` if the returned documents could | |
be instances of ``Employee``. Autoloading cannot resolve | |
the discriminator value ``"Worker"`` to return the document as an | |
instance of ``Employee``. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.