File tree 2 files changed +13
-6
lines changed
2 files changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -847,8 +847,13 @@ coerced to the type of the field. For example:
847
847
User.new(name: [ "hello" ])
848
848
849
849
Assigning an array to a field of type Integer doesn't work since an array can't
850
- be coerced to an Integer. The assignment of uncastable values to a field will
851
- cause a ``nil`` to be written:
850
+ be coerced to an Integer. Internally, Mongoid uses ``Mongoid::RawValue(object)``
851
+ to represent such uncastable values. You can determine how to handle such values
852
+ using the global config ``Mongoid.strict_type_assignment`` and the ``field :strict``
853
+ option.
854
+
855
+ By default the assignment of uncastable values to a field will cause a ``nil``
856
+ to be assigned:
852
857
853
858
.. code::
854
859
@@ -1008,9 +1013,9 @@ setter methods for fields of your custom type.
1008
1013
1009
1014
.. note::
1010
1015
1011
- The ``mongoize`` method should return ``nil`` on values that are uncastable to
1012
- your custom type. See the secion on :ref:`Uncastable Values <uncastable-values>`
1013
- for more details.
1016
+ The ``mongoize`` method should return ``Mongoid::RawValue(object, "YourTypeName")``
1017
+ on values that are uncastable to your custom type. See the section on
1018
+ :ref:`Uncastable Values <uncastable-values>` for more details.
1014
1019
1015
1020
The class method ``demongoize`` does the inverse of ``mongoize``. It takes the raw object
1016
1021
from the MongoDB Ruby driver and converts it to an instance of your custom type.
Original file line number Diff line number Diff line change @@ -16,7 +16,9 @@ def mongoize(object)
16
16
if value . is_a? ( Mongoid ::RawValue )
17
17
case strict
18
18
when :error then value . raise_error!
19
- when :warn then value . warn
19
+ when :warn then value . warn and return nil
20
+ when :suppress then return nil
21
+ # when :defer, assign the Mongoid::RawValue and fail when trying to persist.
20
22
end
21
23
end
22
24
value
You can’t perform that action at this time.
0 commit comments