Skip to content

Commit fb15fd3

Browse files
p-mongopalcaeus
authored
Fix MONGOID-5033 Cannot use any_of with multiple conditions when using symbol operators on Time fields and passing Date instances (#4936)
* Fix MONGOID-5033 Cannot use any_of with multiple conditions when using symbol operators on Time fields and passing Date instances * Update docs/tutorials/mongoid-queries.txt Co-authored-by: Andreas Braun <[email protected]> * Update docs/tutorials/mongoid-queries.txt Co-authored-by: Andreas Braun <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]> Co-authored-by: Andreas Braun <[email protected]>
1 parent 988334a commit fb15fd3

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

source/tutorials/mongoid-documents.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,31 @@ or else the values will not store properly.
377377
end
378378
end
379379

380+
381+
Time Fields
382+
-----------
383+
384+
``Time`` fields store values as ``Time`` instances in the :ref:`configured
385+
time zone <time-zones>`.
386+
387+
``Date`` and ``DateTime`` instances are converted to ``Time`` instances upon
388+
assignment to a ``Time`` field:
389+
390+
.. code-block:: ruby
391+
392+
class Voter
393+
include Mongoid::Document
394+
395+
field :registered_at, type: Time
396+
end
397+
398+
Voter.new(registered_at: Date.today)
399+
# => #<Voter _id: 5fdd80392c97a618f07ba344, registered_at: 2020-12-18 05:00:00 UTC>
400+
401+
In the above example, the value was interpreted as the beginning of today in
402+
local time, because the application was not configured to use UTC times.
403+
404+
380405
Date Fields
381406
-----------
382407

source/tutorials/mongoid-queries.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,63 @@ It is also possible to query using PCRE syntax by constructing
14781478
# => #<Band _id: 5dc9f7d5ce4ef34893354323, name: "Sun Project", description: "Sun\nProject">
14791479

14801480

1481+
Conditions On Fields
1482+
====================
1483+
1484+
When a condition uses a field defined in the model, the value being specified
1485+
in the condition is converted according to the rules of the field, if any.
1486+
For example, consider the following model definition that contains a ``Time``
1487+
field, a ``Date`` field and an implicit ``Object`` field, and also
1488+
intentionally does not define a field called ``deregistered_at``:
1489+
1490+
.. code-block:: ruby
1491+
1492+
class Voter
1493+
include Mongoid::Document
1494+
1495+
field :born_on, type: Date
1496+
field :registered_at, type: Time
1497+
field :voted_at
1498+
end
1499+
1500+
Queries on ``born_on`` and ``registered_at`` fields using ``Date`` and ``Time``
1501+
values, respectively, are straightforward:
1502+
1503+
.. code-block:: ruby
1504+
1505+
Voter.where(born_on: Date.today).selector
1506+
# => {"born_on"=>2020-12-18 00:00:00 UTC}
1507+
1508+
Voter.where(registered_at: Time.now).selector
1509+
# => {"registered_at"=>2020-12-19 04:33:36.939788067 UTC}
1510+
1511+
But, note the differences in behavior when providing a ``Date`` instance
1512+
in all possible scenarios:
1513+
1514+
.. code-block:: ruby
1515+
1516+
Voter.where(born_on: Date.today).selector
1517+
# => {"born_on"=>2020-12-18 00:00:00 UTC}
1518+
1519+
Voter.where(registered_at: Date.today).selector
1520+
# => {"registered_at"=>2020-12-18 00:00:00 -0500}
1521+
1522+
Voter.where(voted_at: Date.today).selector
1523+
# => {"voted_at"=>Fri, 18 Dec 2020}
1524+
1525+
Voter.where(deregistered_at: Date.today).selector
1526+
# => {"deregistered_at"=>2020-12-18 00:00:00 UTC}
1527+
1528+
When using the ``registered_at`` field which is of type ``Time``, the date
1529+
was interpreted to be in local time (as per the :ref:`configured time zone
1530+
<time-zones>`). When using the ``born_on`` field which is of type ``Date``,
1531+
the date was interpreted to be in UTC. When using the ``voted_at`` field
1532+
which was defined without a type (hence implicitly as an ``Object``),
1533+
the date was used unmodified in the constructed query. When using a
1534+
nonexistent field ``deregistered_at`` the date was interpreted to be in UTC
1535+
and converted to a time, matching the behavior of querying a ``Date`` field.
1536+
1537+
14811538
Queries + Persistence
14821539
=====================
14831540

0 commit comments

Comments
 (0)