Skip to content

Commit 5f825ec

Browse files
neilshwekyp-mongo
andauthored
MONGOID-5456 Add documentation for responding to to_* (#5434)
* MONGOID-5456 Add documentation for responding to to_* * Apply suggestions from code review Co-authored-by: Oleg Pudeyev <39304720+p-mongo@users.noreply.github.com> Co-authored-by: Oleg Pudeyev <39304720+p-mongo@users.noreply.github.com>
1 parent 97397b0 commit 5f825ec

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

docs/reference/fields.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ type. Such fields are untyped:
108108

109109
class Product
110110
include Mongoid::Document
111-
111+
112112
field :properties
113113
# Equivalent to:
114114
field :properties, type: Object
@@ -124,7 +124,7 @@ ahead of time:
124124
product = Product.new(properties: "color=white,size=large")
125125
product.properties
126126
# => "color=white,size=large"
127-
127+
128128
product = Product.new(properties: {color: "white", size: "large"})
129129
product.properties
130130
# => {:color=>"white", :size=>"large"}
@@ -914,6 +914,15 @@ Note that the original uncastable values will be stored in the
914914
user.attributes_before_type_cast["name"]
915915
# => ["Mike", "Trout"]
916916

917+
.. note::
918+
919+
Note that for numeric fields, any class that defines ``to_i`` for Integer
920+
fields, ``to_f`` for Floats, and ``to_d`` for BigDecimals, is castable.
921+
Strings are the exception and will only call the corresponding ``to_*``
922+
method if the string is numeric. If a class only defines ``to_i`` and not
923+
``to_f`` and is being assigned to a Float field, this is uncastable, and Mongoid
924+
will not perform a two-step conversion (i.e. ``to_i`` and then ``to_f``).
925+
917926

918927
Reading Uncastable Values
919928
`````````````````````````

0 commit comments

Comments
 (0)