Skip to content

Commit 7de011e

Browse files
feedback
1 parent 8f6a5f1 commit 7de011e

File tree

1 file changed

+73
-68
lines changed

1 file changed

+73
-68
lines changed

source/data-modeling/associations.txt

Lines changed: 73 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ this guide, you can learn about the different types of associations that
2525
{+odm+} supports and how to use them in your application.
2626

2727
Referenced Associations
28-
------------------------
28+
-----------------------
2929

3030
Referenced associations allow you to create a relationship between two models
3131
where one model references the other. {+odm+} supports the following referenced
@@ -516,17 +516,16 @@ method to remove all embedded ``Album`` documents from the ``Band`` class:
516516
:start-after: # start-embedded-destroy-all
517517
:end-before: # end-embedded-destroy-all
518518

519-
Association Behaviors
520-
---------------------
519+
Customize Association Behavior
520+
------------------------------
521521

522-
When working with associations in {+odm+}, there are various behaviors that
523-
allow you to customize how associations are handled in your application. The following
524-
sections describe common association behaviors.
522+
You can use {+odm+} to customize how associations behave in your application.
523+
The following sections describe ways to customize association behaviors.
525524

526525
Extensions
527526
~~~~~~~~~~
528527

529-
Extensions allow you to add specific functionality to an association. You can
528+
Extensions allow you to add custom functionality to an association. You can
530529
define an extension on an association by specifying a block in the association
531530
definition, as shown in the following example:
532531

@@ -540,8 +539,8 @@ Custom Association Names
540539
~~~~~~~~~~~~~~~~~~~~~~~~
541540

542541
You can use the ``class_name`` macro to specify a custom class name for an
543-
association. This is useful when you want to use a different name for an
544-
association than the name of the class. The following example uses the
542+
association. This is useful when you want to name the association something other
543+
than the name of the class. The following example uses the
545544
``class_name`` macro to specify that an embedded association called ``records``
546545
represents the ``Album`` class:
547546

@@ -554,7 +553,7 @@ Custom Keys
554553
~~~~~~~~~~~
555554

556555
By default, {+odm+} uses the ``_id`` field of the parent class when looking up
557-
associations. You can specify a different field to use by specifying the
556+
associations. You can specify different fields to use by using the
558557
``primary_key`` and ``foreign_key`` macros. The following example specifies a new
559558
primary and foreign key for the ``albums`` association on a ``Band`` class:
560559

@@ -564,7 +563,7 @@ primary and foreign key for the ``albums`` association on a ``Band`` class:
564563
:end-before: # end-custom-keys
565564

566565
If you are specifying a ``has_and_belongs_to_many`` association, you can also
567-
set the ``inverse_primary_key`` and ``inverse_foreign_key`` macros. The
566+
use the ``inverse_primary_key`` and ``inverse_foreign_key`` macros. The
568567
``inverse_primary_key`` macro specifies the field on the local model that the
569568
remote model uses to look up the documents.
570569
The ``inverse_foreign_key`` macro specifies the field on the remote model
@@ -582,8 +581,8 @@ The following example specifies a new primary and foreign key for the
582581
Custom Scopes
583582
~~~~~~~~~~~~~
584583

585-
You can set a specific scope on an association by using the ``scope`` parameter.
586-
The ``scope`` parameter determines which documents {+odm+} considers to be part
584+
You can specify the scope of an association by using the ``scope`` parameter.
585+
The ``scope`` parameter determines the documents that {+odm+} considers part
587586
of an association. A scoped association returns only documents that match the
588587
scope conditions when queried. You can set the ``scope`` to either a ``Proc`` with an arity
589588
of zero or a ``Symbol`` that references a named scope on the associated model.
@@ -598,15 +597,15 @@ The following example sets custom scopes on associations in a ``Band`` class:
598597

599598
You can add documents that do not match the scope conditions to an
600599
association. {+odm+} saves the documents to the database and they will appear
601-
in associated memory, however, the documents are not present when the
602-
association is queried in the future.
600+
in associated memory, however, you won't see the documents when querying the
601+
association.
603602

604603
Validations
605604
~~~~~~~~~~~
606605

607-
By default, {+odm+} validates that the children of any associations are present
608-
when it loads the association into memory by using the ``validates_associated``
609-
macro. This applies to the following association types:
606+
When {+odm+} loads an association into memory, by default, it uses the
607+
``validates_associated`` macro to validate that any children are also present.
608+
{+odm+} validates children for the following association types:
610609

611610
- ``embeds_many``
612611
- ``embeds_one``
@@ -650,7 +649,7 @@ In the preceding example, the ``:featured`` association in the ``Band`` class ca
650649
Custom Polymorphic Types
651650
````````````````````````
652651

653-
Starting in version 9.0.2, {+odm+} adds support for custom polymorphic types through
652+
Starting in version 9.0.2, {+odm+} supports custom polymorphic types through
654653
a global registry. You can specify alternative keys to represent different
655654
classes, decoupling your code from the data. The following example specifies
656655
the string ``"artist"`` as an alternate key for the ``Band`` class:
@@ -675,9 +674,9 @@ only for looking up records. This allows you to refactor your
675674
code without breaking the associations in your data.
676675

677676
Polymorphic type aliases are global. The keys you specify must be unique across your
678-
entire code base. However, it is possible to register alternative resolvers that
677+
entire code base. However, you can register alternative resolvers that
679678
can be used for different subsets of your models. In this case, the keys must
680-
only be unique for each resolver. The following example shows how to register
679+
be unique only for each resolver. The following example shows how to register
681680
alternate resolvers:
682681

683682
.. literalinclude:: /includes/data-modeling/association-behaviors.rb
@@ -686,7 +685,7 @@ alternate resolvers:
686685
:end-before: # end-polymorphic-resolvers
687686

688687
Both ``Music::Band`` and ``Tools::Band`` are aliased as
689-
``"bnd"``, but use their own resolver to avoid conflicts.
688+
``"bnd"``, but each model uses its own resolver to avoid conflicts.
690689

691690
Dependent Behavior
692691
~~~~~~~~~~~~~~~~~~
@@ -707,7 +706,7 @@ the following options:
707706

708707
If you don't specify any ``dependent`` options, {+odm+} leaves the child
709708
document unchanged when the parent document is deleted. The child document
710-
continues to reference the deleted parent document. If the child document is
709+
continues to reference the deleted parent document, and if it is
711710
referenced through only the parent, the child document becomes orphaned.
712711

713712
The following example specifies ``dependent`` options on the ``Band`` class:
@@ -721,9 +720,10 @@ Autosave Referenced Associations
721720
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
722721

723722
By default, {+odm+} does not automatically save associated documents from
724-
non-embedded associations when saving the parent document. Because of this, it's
725-
possible to create dangling references to documents that don't exist through
726-
associations. You can use the ``autosave`` option on a referenced association to
723+
non-embedded associations when saving the parent document. This can
724+
result in dangling references to documents that don't exist.
725+
726+
You can use the ``autosave`` option on a referenced association to
727727
automatically save associated documents when saving the parent document. The
728728
following example creates a ``Band`` class with an associated ``Album`` class
729729
and specifies the ``autosave`` option:
@@ -758,21 +758,23 @@ option to an association on the ``Band`` class:
758758
Touch
759759
~~~~~
760760

761-
You can add the ``touch`` option to any ``belongs_to`` association to ensure
762-
that {+odm+} touches the parent document any time the child document is updated.
763-
When {+odm+} touches the parent document, it updates the document's
764-
``updated_at`` field. The following example adds the ``touch`` option to an
765-
association on the ``Band`` class:
761+
When {+odm+} *touches* a document, it updates the document's
762+
``updated_at`` field to the current date and time. You can add the ``touch``
763+
option to any ``belongs_to`` association to ensure that {+odm+} touches the
764+
parent document whenever the child document is updated. The following example
765+
adds the ``touch`` option to an association on the ``Band`` class:
766766

767767
.. literalinclude:: /includes/data-modeling/association-behaviors.rb
768768
:language: ruby
769769
:start-after: # start-touch
770770
:end-before: # end-touch
771771

772-
You can also specify a string or a symbol in the ``touch`` option to specify
773-
another field to touch on the parent association. When doing so, {+odm+} updates
774-
the specified field and the ``updated_at`` field in the parent association. The
775-
following example instructs {+odm+} to touch the ``bands_updated_at`` field:
772+
You can also use the ``touch`` option to specify another field on the parent
773+
association, as a string or a symbol. When {+odm+} touches the parent
774+
association, it sets both the ``updated_at`` field and the specified field
775+
to the current date and time.
776+
777+
The following example instructs {+odm+} to touch the ``bands_updated_at`` field:
776778

777779
.. literalinclude:: /includes/data-modeling/association-behaviors.rb
778780
:language: ruby
@@ -783,18 +785,21 @@ following example instructs {+odm+} to touch the ``bands_updated_at`` field:
783785

784786
In embedded associations, when an embedded document is touched, {+odm+}
785787
touches its parents recursively. Because of this, adding a ``touch``
786-
attribute to an ``embedded_in`` association is unnecessary. {+odm+} does not
787-
support specifying additional fields to touch in ``embedded_in``
788-
associations.
788+
attribute to an ``embedded_in`` association is unnecessary.
789+
790+
{+odm+} does not support specifying additional fields to touch in
791+
``embedded_in`` associations.
789792

790793
Counter Cache
791794
~~~~~~~~~~~~~
792795

793796
You can use the ``counter_cache`` option to store the number of objects
794-
that belong to an associated field. When specifying this option, {+odm+} stores
797+
that belong to an associated field. When you specify this option, {+odm+} stores
795798
an extra attribute on the associated models to store the count. Because of this,
796799
you must specify the ``Mongoid::Attributes::Dynamic`` module in the associated
797-
classes. The following example adds the ``counter_cache`` option to a ``Band``
800+
classes.
801+
802+
The following example adds the ``counter_cache`` option to a ``Band``
798803
class and specifies the ``Mongoid::Attributes::Dynamic`` in a ``Label`` class:
799804

800805
.. literalinclude:: /includes/data-modeling/association-behaviors.rb
@@ -828,9 +833,9 @@ Attributes
828833
All associations contain attributes that store information about the associated
829834
document. Associations contain the following attributes:
830835

831-
- ``_target``: Contains the proxied document or documents
832-
- ``_base``: Contains the document on which the association is defined
833-
- ``_association``: Contains information about the association
836+
- ``_target``: The proxied document or documents
837+
- ``_base``: The document on which the association is defined
838+
- ``_association``: Information about the association
834839

835840
The following example accesses each of the preceding attributes:
836841

@@ -849,7 +854,7 @@ attribute:
849854
* - Method
850855
- Description
851856
* - ``Association#as``
852-
- Returns the name of the parent to a polymorphic child.
857+
- The name of the parent to a polymorphic child.
853858
* - ``Association#as?``
854859
- Returns whether an ``as`` option exists.
855860
* - ``Association#autobuilding?``
@@ -859,66 +864,66 @@ attribute:
859864
* - ``Association#cascading_callbacks?``
860865
- Returns whether the association has callbacks cascaded down from the parent.
861866
* - ``Association#class_name``
862-
- Returns the class name of the proxied document.
867+
- The class name of the proxied document.
863868
* - ``Association#cyclic?``
864869
- Returns whether the association is a cyclic association.
865870
* - ``Association#dependent``
866-
- Returns the association's dependent option.
871+
- The association's dependent option.
867872
* - ``Association#destructive?``
868873
- Returns ``true`` if the association has a dependent delete or destroy method.
869874
* - ``Association#embedded?``
870875
- Returns whether the association is embedded in another document.
871876
* - ``Association#forced_nil_inverse?``
872877
- Returns whether the association has a ``nil`` inverse defined.
873878
* - ``Association#foreign_key``
874-
- Returns the name of the foreign-key field.
879+
- The name of the foreign-key field.
875880
* - ``Association#foreign_key_check``
876-
- Returns the name of the foreign-key field's dirty-check method.
881+
- The name of the foreign-key field's dirty-check method.
877882
* - ``Association#foreign_key_setter``
878-
- Returns the name of the foreign-key field's setter.
883+
- The name of the foreign-key field's setter.
879884
* - ``Association#indexed?``
880885
- Returns whether the foreign key is auto indexed.
881886
* - ``Association#inverses``
882-
- Returns the names of all inverse associations.
887+
- The names of all inverse associations.
883888
* - ``Association#inverse``
884-
- Returns the name of a single inverse association.
889+
- The name of a single inverse association.
885890
* - ``Association#inverse_class_name``
886-
- Returns the class name of the association on the inverse side.
891+
- The class name of the association on the inverse side.
887892
* - ``Association#inverse_foreign_key``
888-
- Returns the name of the foreign-key field on the inverse side.
893+
- The name of the foreign-key field on the inverse side.
889894
* - ``Association#inverse_klass``
890-
- Returns the class of the association on the inverse side.
895+
- The class of the association on the inverse side.
891896
* - ``Association#inverse_association``
892-
- Returns the metadata of the association on the inverse side.
897+
- The metadata of the association on the inverse side.
893898
* - ``Association#inverse_of``
894-
- Returns the explicitly defined name of the inverse association.
899+
- The explicitly defined name of the inverse association.
895900
* - ``Association#inverse_setter``
896-
- Returns the name of the method used to set the inverse.
901+
- The name of the method used to set the inverse.
897902
* - ``Association#inverse_type``
898-
- Returns the name of the polymorphic-type field of the inverse.
903+
- The name of the polymorphic-type field of the inverse.
899904
* - ``Association#inverse_type_setter``
900-
- Returns the name of the polymorphic-type field's setter of the inverse.
905+
- The name of the polymorphic-type field's setter of the inverse.
901906
* - ``Association#key``
902-
- Returns the name of the field in the attribute's hash used to get the association.
907+
- The name of the field in the attribute's hash that is used to get the association.
903908
* - ``Association#klass``
904-
- Returns the class of the proxied documents in the association.
909+
- The class of the proxied documents in the association.
905910
* - ``Association#name``
906-
- Returns the association name.
911+
- The association name.
907912
* - ``Association#options``
908-
- Returns self, for API compatibility with ActiveRecord.
913+
- Returns ``self``, for API compatibility with ActiveRecord.
909914
* - ``Association#order``
910-
- Returns the custom sorting options on the association.
915+
- The custom sorting options on the association.
911916
* - ``Association#polymorphic?``
912917
- Returns whether the association is polymorphic.
913918
* - ``Association#setter``
914-
- Returns the name of the field to set the association.
919+
- The name of the field to set the association.
915920
* - ``Association#store_as``
916-
- Returns the name of the attribute in which to store an embedded association.
921+
- The name of the attribute in which to store an embedded association.
917922
* - ``Association#touchable?``
918923
- Returns whether the association has a touch option.
919924
* - ``Association#type``
920-
- Returns the name of the field to get the polymorphic type.
925+
- The name of the field to get the polymorphic type.
921926
* - ``Association#type_setter``
922-
- Returns the name of the field to set the polymorphic type.
927+
- The name of the field to set the polymorphic type.
923928
* - ``Association#validate?``
924929
- Returns whether the association has an associated validation.

0 commit comments

Comments
 (0)