Skip to content

Commit eeccd5d

Browse files
committed
More tweaks
1 parent 7f2bb54 commit eeccd5d

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

docs/reference/fields.txt

+10-5
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,13 @@ coerced to the type of the field. For example:
847847
User.new(name: [ "hello" ])
848848

849849
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:
852857

853858
.. code::
854859

@@ -1008,9 +1013,9 @@ setter methods for fields of your custom type.
10081013

10091014
.. note::
10101015

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.
10141019

10151020
The class method ``demongoize`` does the inverse of ``mongoize``. It takes the raw object
10161021
from the MongoDB Ruby driver and converts it to an instance of your custom type.

lib/mongoid/fields/standard.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def mongoize(object)
1616
if value.is_a?(Mongoid::RawValue)
1717
case strict
1818
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.
2022
end
2123
end
2224
value

0 commit comments

Comments
 (0)