File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -1577,6 +1577,7 @@ To have Mongoid raise an error when a scope would overwrite an existing class
1577
1577
method, set the ``scope_overwrite_exception`` :ref:`configuration option
1578
1578
<configuration-options>` to ``true``.
1579
1579
1580
+
1580
1581
Default Scopes
1581
1582
--------------
1582
1583
@@ -1705,6 +1706,35 @@ that would affect its visibility within the scoped association.
1705
1706
label.reload.bands # []
1706
1707
1707
1708
1709
+ Runtime Default Scope Override
1710
+ ------------------------------
1711
+
1712
+ You can use the ``with_scope`` method to change the default scope in a block
1713
+ at runtime:
1714
+
1715
+ .. code-block:: ruby
1716
+
1717
+ class Band
1718
+ include Mongoid::Document
1719
+ field :country, type: String
1720
+ field :genres, type: Array
1721
+
1722
+ scope :english, ->{ where(country: "England") }
1723
+ end
1724
+
1725
+ criteria = Band.with_scope(Band.english) do
1726
+ Band.all
1727
+ end
1728
+
1729
+ criteria
1730
+ # =>
1731
+ # #<Mongoid::Criteria
1732
+ # selector: {"country"=>"England"}
1733
+ # options: {}
1734
+ # class: Band
1735
+ # embedded: false>
1736
+
1737
+
1708
1738
Class Methods
1709
1739
-------------
1710
1740
You can’t perform that action at this time.
0 commit comments