Skip to content

Commit f728456

Browse files
johnnyshieldspcomandeo-mongo
authored
MONGOID-5128 Scoped associations (#5017)
* MONGOID-5128 Support association :scope option * typo fix * expand documentation * Fix specs * Fix broken specs * expand docs * release note * remove broken link * Revert "remove broken link" This reverts commit 8e4fea74f1bdf881f8b3fd0cda742ba01bd600e4. * add missing labels Co-authored-by: shields <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]> Co-authored-by: Dmitry Rybakov <[email protected]>
1 parent 5d06340 commit f728456

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

source/reference/associations.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,61 @@ the model. However, since Mongoid doesn't know what type of data should be
969969
allowed in the field, the field is created with a type of Object. It is a
970970
good idea to explicitly define the field with the appropriate type.
971971

972+
973+
.. _association-scope:
974+
975+
Custom Scopes
976+
-------------
977+
978+
You may set a specific scope on an association using the ``:scope`` parameter.
979+
The scope is an additional filter that restricts which objects are considered
980+
to be a part of the association - a scoped association will return only
981+
documents which satisfy the scope condition.. The scope may be either:
982+
983+
- a ``Proc`` with arity zero, or
984+
- a ``Symbol`` which references a :ref:`named scope <named-scopes>` on the
985+
associated model.
986+
987+
.. code-block:: ruby
988+
class Trainer
989+
has_many :pets, scope: -> { where(species: 'dog') }
990+
has_many :toys, scope: :rubber
991+
end
992+
993+
class Pet
994+
belongs_to :trainer
995+
end
996+
997+
class Toy
998+
scope :rubber, where(material: 'rubber')
999+
belongs_to :trainer
1000+
end
1001+
1002+
.. note::
1003+
1004+
It is possible to add documents that do not satisfy an association's scope
1005+
to that association. In this case, such documents will appear associated
1006+
in memory, and will be saved to the database, but will not be present when
1007+
the association is queried in the future. For example:
1008+
1009+
.. code-block:: ruby
1010+
1011+
trainer = Trainer.create!
1012+
dog = Pet.create!(trainer: trainer, species: 'dog')
1013+
cat = Pet.create!(trainer: trainer, species: 'cat')
1014+
1015+
trainer.pets #=> [dog, cat]
1016+
1017+
trainer.reload.pets #=> [dog]
1018+
1019+
.. note::
1020+
1021+
Mongoid's syntax for scoped association differs from that of Active Record.
1022+
Mongoid uses the ``:scope`` keyword argument for consistency with other
1023+
association options, whereas in Active Record the scope is a positional
1024+
argument.
1025+
1026+
9721027
Validations
9731028
-----------
9741029

source/reference/queries.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,8 @@ as its ``id`` alias) cannot be omitted via ``without``:
880880
# embedded: false>
881881

882882

883+
.. _ordering:
884+
883885
Ordering
884886
========
885887

@@ -1032,7 +1034,7 @@ to be combined with :ref:`ordering <ordering>` to ensure consistent results.
10321034
.. _batch-size:
10331035

10341036
``batch_size``
1035-
--------
1037+
--------------
10361038

10371039
When executing large queries, or when iterating over query results with an enumerator method such as
10381040
``Criteria#each``, Mongoid automatically uses the `MongoDB getMore command
@@ -1829,6 +1831,9 @@ Scoping
18291831
Scopes provide a convenient way to reuse common criteria with more
18301832
business domain style syntax.
18311833

1834+
1835+
.. _named-scopes:
1836+
18321837
Named Scopes
18331838
------------
18341839

source/release-notes/mongoid-7.4.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ Mongoid 7.4 and later will inherit the new implementation provided by
113113
implementation returning a hash of ``{"$oid" => "..."}``.
114114

115115

116+
Scoped Associations
117+
-------------------
118+
119+
Associations now support the ``:scope`` argument, yielding
120+
:ref:`scoped associations <association-scope>`.
121+
122+
116123
``distinct`` and ``pluck`` Respect Field Aliases In Embedded Documents
117124
----------------------------------------------------------------------
118125

0 commit comments

Comments
 (0)