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.
368368 # to parent contexts by default. (default: false)
369369 join_contexts: false
370370
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+
371377 # Maintain legacy behavior of pluck and distinct, which does not demongoize
372378 # values on returning them. Setting this option to false will cause
373379 # 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:
8787Mongoid 7.5 fixes incorrect usage of the driver's ``update_one`` method from
8888Mongoid's ``upsert`` method. Mongoid's ``upsert`` actually performs a
8989replacing 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
3131**Breaking change:** The following options have had their default values
3232changed in Mongoid 8.0:
3333
34- - ``:overwrite_chained_operators`` => ``false``
3534- ``:broken_aggregables`` => ``false``
3635- ``:broken_alias_handling`` => ``false``
3736- ``:broken_and`` => ``false``
3837- ``:broken_scoping`` => ``false``
3938- ``:broken_updates`` => ``false``
4039- ``:compare_time_by_ms`` => ``true``
40+ - ``:legacy_attributes`` => true
4141- ``:legacy_pluck_distinct`` => ``false``
4242- ``:legacy_triple_equals`` => ``false``
4343- ``:map_big_decimal_to_decimal128`` => ``true``
4444- ``:object_id_as_json_oid`` => ``false``
45+ - ``:overwrite_chained_operators`` => ``false``
4546
4647Please refer to :ref:`configuration option <configuration-options>` for
4748the description and effects of each of these options.
You can’t perform that action at this time.
0 commit comments