File tree Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -368,6 +368,12 @@ for details on driver options.
368
368
# to parent contexts by default. (default: false)
369
369
join_contexts: false
370
370
371
+ # When this flag is true, the attributes method on a document will return
372
+ # a BSON::Document when that document is retrieved from the database, and
373
+ # a Hash otherwise. When this flag is false, the attributes method will
374
+ # always return a Hash. (default: false)
375
+ #legacy_attributes: true
376
+
371
377
# Maintain legacy behavior of pluck and distinct, which does not demongoize
372
378
# values on returning them. Setting this option to false will cause
373
379
# pluck and distinct to return demongoized values. Setting this option to
Original file line number Diff line number Diff line change @@ -87,3 +87,42 @@ The following functions are affected by this change:
87
87
Mongoid 7.5 fixes incorrect usage of the driver's ``update_one`` method from
88
88
Mongoid's ``upsert`` method. Mongoid's ``upsert`` actually performs a
89
89
replacing upsert, and Mongoid 7.5 correctly calls ``replace_one``.
90
+
91
+
92
+ Force the ``attributes`` Method to Always Return a ``Hash``
93
+ ```````````````````````````````````````````````````````````
94
+
95
+ Mongoid 7.5 with the ``Mongoid.legacy_attributes`` option set to ``false``
96
+ will always return a ``Hash`` when calling the ``attributes`` method.
97
+ For example:
98
+
99
+ .. code-block:: ruby
100
+
101
+ class Band
102
+ include Mongoid::Document
103
+
104
+ field :name
105
+ end
106
+
107
+ band = Band.create!(name: "The Rolling Stones")
108
+ p band.attributes.class
109
+ # => Hash
110
+
111
+ band = Band.first
112
+ p band.attributes.class
113
+ # => Hash
114
+
115
+ In Mongoid 7.4 and earlier, and in 7.5 with the ``Mongoid.legacy_attributes``
116
+ option set to ``true``, the ``attributes`` method on a document will return a
117
+ ``BSON::Document`` when retrieving that document from the database, but will
118
+ return a ``Hash`` when instantiating a new document:
119
+
120
+ .. code-block:: ruby
121
+
122
+ band = Band.create!(name: "The Rolling Stones")
123
+ p band.attributes.class
124
+ # => Hash
125
+
126
+ band = Band.first
127
+ p band.attributes.class
128
+ # => BSON::Document
Original file line number Diff line number Diff line change @@ -31,17 +31,18 @@ Default Option Values Changed
31
31
**Breaking change:** The following options have had their default values
32
32
changed in Mongoid 8.0:
33
33
34
- - ``:overwrite_chained_operators`` => ``false``
35
34
- ``:broken_aggregables`` => ``false``
36
35
- ``:broken_alias_handling`` => ``false``
37
36
- ``:broken_and`` => ``false``
38
37
- ``:broken_scoping`` => ``false``
39
38
- ``:broken_updates`` => ``false``
40
39
- ``:compare_time_by_ms`` => ``true``
40
+ - ``:legacy_attributes`` => true
41
41
- ``:legacy_pluck_distinct`` => ``false``
42
42
- ``:legacy_triple_equals`` => ``false``
43
43
- ``:map_big_decimal_to_decimal128`` => ``true``
44
44
- ``:object_id_as_json_oid`` => ``false``
45
+ - ``:overwrite_chained_operators`` => ``false``
45
46
46
47
Please refer to :ref:`configuration option <configuration-options>` for
47
48
the description and effects of each of these options.
You can’t perform that action at this time.
0 commit comments