Skip to content

Commit 8f6a5f1

Browse files
edits
1 parent 002026b commit 8f6a5f1

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

source/data-modeling/associations.txt

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -519,15 +519,15 @@ method to remove all embedded ``Album`` documents from the ``Band`` class:
519519
Association Behaviors
520520
---------------------
521521

522-
When working with associations in {+odm+}, there are various behaviors you can
523-
use to customize how associations are handled in your application. The following
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
524524
sections describe common association behaviors.
525525

526526
Extensions
527527
~~~~~~~~~~
528528

529529
Extensions allow you to add specific functionality to an association. You can
530-
define an extension on an association by specifying a block to the association
530+
define an extension on an association by specifying a block in the association
531531
definition, as shown in the following example:
532532

533533
.. literalinclude:: /includes/data-modeling/association-behaviors.rb
@@ -556,7 +556,7 @@ Custom Keys
556556
By default, {+odm+} uses the ``_id`` field of the parent class when looking up
557557
associations. You can specify a different field to use by specifying the
558558
``primary_key`` and ``foreign_key`` macros. The following example specifies a new
559-
primary and foreign key for the ``Band`` and ``Album`` classes:
559+
primary and foreign key for the ``albums`` association on a ``Band`` class:
560560

561561
.. literalinclude:: /includes/data-modeling/association-behaviors.rb
562562
:language: ruby
@@ -566,9 +566,9 @@ primary and foreign key for the ``Band`` and ``Album`` classes:
566566
If you are specifying a ``has_and_belongs_to_many`` association, you can also
567567
set the ``inverse_primary_key`` and ``inverse_foreign_key`` macros. The
568568
``inverse_primary_key`` macro specifies the field on the local model that the
569-
remote model uses to look up the local model documents, and
570-
the ``inverse_foreign_key`` macro specifies the field on the remote model
571-
storing the values in ``inverse_primary_key``.
569+
remote model uses to look up the documents.
570+
The ``inverse_foreign_key`` macro specifies the field on the remote model
571+
that stores the values found in ``inverse_primary_key``.
572572

573573
The following example specifies a new primary and foreign key for the
574574
``Band`` and ``Members`` classes in a ``has_and_belongs_to_many`` association:
@@ -577,6 +577,7 @@ The following example specifies a new primary and foreign key for the
577577
:language: ruby
578578
:start-after: # start-custom-inverse-keys
579579
:end-before: # end-custom-inverse-keys
580+
:emphasize-lines: 9, 20
580581

581582
Custom Scopes
582583
~~~~~~~~~~~~~
@@ -585,7 +586,7 @@ You can set a specific scope on an association by using the ``scope`` parameter.
585586
The ``scope`` parameter determines which documents {+odm+} considers to be part
586587
of an association. A scoped association returns only documents that match the
587588
scope conditions when queried. You can set the ``scope`` to either a ``Proc`` with an arity
588-
of zero, or a ``Symbol`` that references a named scope on the associated model.
589+
of zero or a ``Symbol`` that references a named scope on the associated model.
589590
The following example sets custom scopes on associations in a ``Band`` class:
590591

591592
.. literalinclude:: /includes/data-modeling/association-behaviors.rb
@@ -624,7 +625,7 @@ You can turn off this validation behavior by setting the ``validate`` macro to
624625
Polymorphism
625626
~~~~~~~~~~~~
626627

627-
{+odm+} supports polymorphism on the child class in one-to-one and one-to-many associations.
628+
{+odm+} supports polymorphism on the child classes of one-to-one and one-to-many associations.
628629
Polymorphic associations allows a single association to contain objects of different class
629630
types. You can define a polymorphic association by setting the ``polymorphic``
630631
option to ``true`` in a child association and adding the ``as`` option to the
@@ -674,8 +675,8 @@ only for looking up records. This allows you to refactor your
674675
code without breaking the associations in your data.
675676

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

@@ -698,7 +699,7 @@ the following options:
698699
callbacks.
699700
- ``destroy``: Deletes the child documents and runs all model callbacks.
700701
- ``nullify``: Sets the foreign key of the child documents to ``nil``. The child
701-
document might become orphaned if it is only referenced by the parent.
702+
document might become orphaned if it is referenced by only the parent.
702703
- ``restrict_with_exception``: Raises an exception if the child document is not
703704
empty.
704705
- ``restrict_with_error``: Cancels the operation and returns ``false`` if the
@@ -707,7 +708,7 @@ the following options:
707708
If you don't specify any ``dependent`` options, {+odm+} leaves the child
708709
document unchanged when the parent document is deleted. The child document
709710
continues to reference the deleted parent document. If the child document is
710-
only referenced through the parent, the child document becomes orphaned.
711+
referenced through only the parent, the child document becomes orphaned.
711712

712713
The following example specifies ``dependent`` options on the ``Band`` class:
713714

@@ -719,13 +720,13 @@ The following example specifies ``dependent`` options on the ``Band`` class:
719720
Autosave Referenced Associations
720721
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
721722

722-
By default, {+odm+} does not automatically save associated documents for
723+
By default, {+odm+} does not automatically save associated documents from
723724
non-embedded associations when saving the parent document. Because of this, it's
724725
possible to create dangling references to documents that don't exist through
725726
associations. You can use the ``autosave`` option on a referenced association to
726727
automatically save associated documents when saving the parent document. The
727728
following example creates a ``Band`` class with an associated ``Album`` class
728-
and specifies the ``autosave`` option on the ``Album`` association:
729+
and specifies the ``autosave`` option:
729730

730731
.. literalinclude:: /includes/data-modeling/association-behaviors.rb
731732
:language: ruby
@@ -782,7 +783,7 @@ following example instructs {+odm+} to touch the ``bands_updated_at`` field:
782783

783784
In embedded associations, when an embedded document is touched, {+odm+}
784785
touches its parents recursively. Because of this, adding a ``touch``
785-
attribute to ``embedded_in`` association is unnecessary. {+odm+} does not
786+
attribute to an ``embedded_in`` association is unnecessary. {+odm+} does not
786787
support specifying additional fields to touch in ``embedded_in``
787788
associations.
788789

@@ -792,7 +793,7 @@ Counter Cache
792793
You can use the ``counter_cache`` option to store the number of objects
793794
that belong to an associated field. When specifying this option, {+odm+} stores
794795
an extra attribute on the associated models to store the count. Because of this,
795-
you must specify the ``Mongoid::Attributes::Dynamic`` module in associated
796+
you must specify the ``Mongoid::Attributes::Dynamic`` module in the associated
796797
classes. The following example adds the ``counter_cache`` option to a ``Band``
797798
class and specifies the ``Mongoid::Attributes::Dynamic`` in a ``Label`` class:
798799

@@ -818,7 +819,7 @@ document. The following example shows how to access metadata by using the
818819

819820
.. note::
820821

821-
Replace ``<association name>`` in the preceding example with the name of your
822+
Replace ``<association_name>`` in the preceding example with the name of your
822823
association.
823824

824825
Attributes
@@ -828,7 +829,7 @@ All associations contain attributes that store information about the associated
828829
document. Associations contain the following attributes:
829830

830831
- ``_target``: Contains the proxied document or documents
831-
- ``_base``: Contains the document that the association is defined on
832+
- ``_base``: Contains the document on which the association is defined
832833
- ``_association``: Contains information about the association
833834

834835
The following example accesses each of the preceding attributes:

source/includes/data-modeling/association-behaviors.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# start-extensions
22
class Band
33
include Mongoid::Document
4+
45
embeds_many :albums do
56
def find_by_name(name)
67
where(name: name).first
@@ -14,6 +15,7 @@ def find_by_name(name)
1415
# start-custom-name
1516
class Band
1617
include Mongoid::Document
18+
1719
embeds_many :records, class_name: "Album"
1820
end
1921
# end-custom-name
@@ -62,10 +64,9 @@ class Member
6264
class Band
6365
include Mongoid::Document
6466

65-
# Only return published albums
6667
has_many :albums, scope: -> { where(published: true) }
6768

68-
# Only return upcoming tours
69+
# Uses a scope called "upcoming" on the Tour model
6970
has_many :tours, scope: :upcoming
7071
end
7172
# end-custom-scope
@@ -184,8 +185,9 @@ class Band
184185

185186
# start-touch-specific
186187
class Band
187-
include Mongoid::Document
188-
belongs_to :label, touch: :bands_updated_at
188+
include Mongoid::Document
189+
190+
belongs_to :label, touch: :bands_updated_at
189191
end
190192
# end-touch-specific
191193

0 commit comments

Comments
 (0)