Skip to content

Commit 036b072

Browse files
p-mongop
andauthored
MONGOID-5326 Document specifying field types as strings/symbols (#5256)
Co-authored-by: Oleg Pudeyev <code@olegp.name>
1 parent e1d5468 commit 036b072

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

source/reference/fields.txt

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ on a person by using the ``field`` macro.
6060
field :weight, type: Float
6161
end
6262

63-
Below is a list of valid types for fields.
63+
The valid types for fields are as follows:
6464

6565
- ``Array``
6666
- ``BigDecimal``
67-
- ``Boolean``
67+
- ``Mongoid::Boolean``, which may be specified simply as ``Boolean`` in the
68+
scope of a class which included ``Mongoid::Document``.
6869
- ``Date``
6970
- ``DateTime``
7071
- ``Float``
@@ -76,10 +77,15 @@ Below is a list of valid types for fields.
7677
- ``Regexp``
7778
- ``Set``
7879
- ``String``
79-
- ``StringifiedSymbol``
80+
- ``Mongoid::StringifiedSymbol``, which may be specified simply as
81+
``StringifiedSymbol`` in the scope of a class which included
82+
``Mongoid::Document``.
8083
- ``Symbol``
8184
- ``Time``
82-
- ``TimeWithZone``
85+
- ``ActiveSupport::TimeWithZone``
86+
87+
Mongoid also recognizes the string ``"Boolean"`` as an alias for the
88+
``Mongoid::Boolean`` class.
8389

8490
To define custom field types, refer to :ref:`Custom Field Types <custom-field-types>` below.
8591

@@ -517,6 +523,47 @@ you have three options:
517523
This query will find all values that are either a ``decimal128`` value or
518524
a string that match that value.
519525

526+
527+
Using Symbols Or Strings Instead Of Classes
528+
-------------------------------------------
529+
530+
Mongoid permits using symbols or strings instead of classes to specify the
531+
type of fields, for example:
532+
533+
.. code-block:: ruby
534+
535+
class Order
536+
include Mongoid::Document
537+
538+
field :state, type: :integer
539+
# Equivalent to:
540+
field :state, type: "integer"
541+
# Equivalent to:
542+
field :state, type: Integer
543+
end
544+
545+
Only standard field types as listed below can be specified using symbols or
546+
strings in this manner. Mongoid recognizes the following expansions:
547+
548+
- ``:array`` => ``Array``
549+
- ``:big_decimal`` => ``BigDecimal``
550+
- ``:binary`` => ``BSON::Binary``
551+
- ``:boolean`` => ``Mongoid::Boolean``
552+
- ``:date`` => ``Date``
553+
- ``:date_time`` => ``DateTime``
554+
- ``:float`` => ``Float``
555+
- ``:hash`` => ``Hash``
556+
- ``:integer`` => ``Integer``
557+
- ``:object_id`` => ``BSON::ObjectId``
558+
- ``:range`` => ``Range``
559+
- ``:regexp`` => ``Regexp``
560+
- ``:set`` => ``Set``
561+
- ``:string`` => ``String``
562+
- ``:stringified_symbol`` => ``StringifiedSymbol``
563+
- ``:symbol`` => ``Symbol``
564+
- ``:time`` => ``Time``
565+
566+
520567
.. _field-default-values:
521568

522569
Specifying Field Default Values
@@ -1209,6 +1256,7 @@ alter the criteria to match the current locale.
12091256
Product.where(description: "Marvelous!")
12101257
# The resulting MongoDB query filter: { "description.en" : "Marvelous!" }
12111258

1259+
12121260
Indexing
12131261
--------
12141262

0 commit comments

Comments
 (0)