/
documents.html
644 lines (641 loc) · 57.5 KB
/
documents.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
<meta content='A Ruby ODM for MongoDB' name='description' />
<meta content='mongoid, mongodb, ruby, rails, odm, durran jordan' name='keywords' />
<!--[if lt IE 9]>
<script src='http://html5shiv.googlecode.com/svn/trunk/html5.js'></script>
<![endif]-->
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0' name='viewport' />
<link href="/stylesheets/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css" />
<link href="/stylesheets/bootstrap-responsive.min.css" media="screen" rel="stylesheet" type="text/css" />
<link href="/stylesheets/mongoid.css" media="screen" rel="stylesheet" type="text/css" />
<link href="/stylesheets/mongoid-coderay.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/javascripts/jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="/javascripts/bootstrap-dropdown.js" type="text/javascript"></script>
<script src="/javascripts/bootstrap-scrollspy.js" type="text/javascript"></script>
<script src="/javascripts/bootstrap-tooltip.js" type="text/javascript"></script>
<script src="/javascripts/mongoid.js" type="text/javascript"></script>
<title>Mongoid: Documents</title>
</head>
<body data-offset='100' data-spy='scroll-spy' data-target='.page-nav'>
<div class='mongoid' id='header'>
<div class='navbar navbar-inverse navbar-fixed-top'>
<div class='navbar-inner'>
<div class='container'>
<ul class='nav'>
<li class='link'>
<a title="Mongoid" href="/en/mongoid/"><img src="/images/mongoid-logo-small-green.png" />
</a>
</li>
<li class='link'>
<a title="Origin" href="/en/origin/"><img src="/images/origin-logo-small-white.png" />
</a>
</li>
<li class='link'>
<a title="Moped" href="/en/moped/"><img src="/images/moped-logo-small-white.png" />
</a>
</li>
</ul>
<div class='logo-text'>
mongoid
</div>
</div>
</div>
</div>
</div>
<div id='navigation'>
<div class='subnav'>
<div class='container'>
<ul class='nav nav-pills mongoid'>
<li><a href="/en/mongoid/">HOME</a></li>
<li class='dropdown' id='docs'>
<a class='dropdown-toggle' data-toggle='dropdown' href='#docs'>
DOCS
<b class='caret'></b>
</a>
<ul class='dropdown-menu'>
<li><a href="/en/mongoid/docs/installation.html">Installation</a></li>
<li><a href="/en/mongoid/docs/documents.html">Documents</a></li>
<li><a href="/en/mongoid/docs/persistence.html">Persistence</a></li>
<li><a href="/en/mongoid/docs/querying.html">Querying</a></li>
<li><a href="/en/mongoid/docs/relations.html">Relations</a></li>
<li><a href="/en/mongoid/docs/nested_attributes.html">Nested Attributes</a></li>
<li><a href="/en/mongoid/docs/identity_map.html">Identity Map</a></li>
<li><a href="/en/mongoid/docs/callbacks.html">Callbacks</a></li>
<li><a href="/en/mongoid/docs/validation.html">Validation</a></li>
<li><a href="/en/mongoid/docs/indexing.html">Indexing</a></li>
<li><a href="/en/mongoid/docs/rails.html">Rails</a></li>
<li><a href="/en/mongoid/docs/extras.html">Extras</a></li>
<li><a href="/en/mongoid/docs/upgrading.html">Upgrading</a></li>
<li><a href="/en/mongoid/docs/contributing.html">Contributing</a></li>
<li><a href="/en/mongoid/docs/performance.html">Performance</a></li>
<li><a href="/en/mongoid/docs/tips.html">Tips/FAQs</a></li>
</ul>
</li>
<li><a href="/en/mongoid/links.html">LINKS</a></li>
<li><a href="/en/mongoid/donate.html">DONATE</a></li>
</ul>
</div>
</div>
</div>
<div id='content'>
<div class='container'>
<h1>Documents</h1>
<p>
Documents are the core objects in Mongoid and any object that is to be
persisted to the database must include <code>Mongoid::Document</code>. The
representation of a Document in MongoDB is a BSON object that is very similar
to a Ruby hash or JSON object. Documents can be stored in their own
collections in the database, or can be embedded in other Documents n levels
deep.
</p>
<div class='page-nav'>
<div class='container'>
<ul class='nav nav-pills'>
<li><a href="#storage">Storage</a></li>
<li><a href="#fields">Fields</a></li>
<li><a href="#dynamic_fields">Dynamic Fields</a></li>
<li><a href="#localized_fields">Localized Fields</a></li>
<li><a href="#dirty">Dirty Tracking</a></li>
<li><a href="#security">Security</a></li>
<li><a href="#readonly">Readonly Attributes</a></li>
<li><a href="#inheritance">Inheritance</a></li>
</ul>
</div>
</div>
<section id='storage'>
<h2>Storage</h2>
<p>
Mongoid by default stores documents in a collection that is the pluralized
form of the class name. For the following <code>Person</code> class, the
collection the document would get stored in would be named <code>people</code>.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Person</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
<span class="keyword">end</span>
</pre></div>
</div>
<p>
Model class name cannot end with "s", because it will be considered as
the pluralized form of the word. For example <code>Status</code> would be considered
as the plural form of <code>Statu</code>, which will cause a few known problems.
</p>
<p>
This is a limitation of the <code>ActiveSupport::Inflector#classify</code> which
Mongoid uses to convert from filenames and collection names to class names.
You can overcome this by specifying a custom inflection rule for your model class.
For example, the following code will take care of the model named <code>Status</code>
</p>
<div class="CodeRay">
<div class="code"><pre><span class="constant">ActiveSupport</span>::<span class="constant">Inflector</span>.inflections <span class="keyword">do</span> |inflect|
 inflect.singular(<span class="string"><span class="delimiter">"</span><span class="content">status</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">status</span><span class="delimiter">"</span></span>)
<span class="keyword">end</span>
</pre></div>
</div>
<p>
The collection for the model's documents can be changed at the class level
if you would like them persisted elsewhere. You can also change the database
and session the model gets persisted in from the defaults.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Person</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 store_in collection: <span class="string"><span class="delimiter">"</span><span class="content">citizens</span><span class="delimiter">"</span></span>, database: <span class="string"><span class="delimiter">"</span><span class="content">other</span><span class="delimiter">"</span></span>, session: <span class="string"><span class="delimiter">"</span><span class="content">secondary</span><span class="delimiter">"</span></span>
<span class="keyword">end</span>
</pre></div>
</div>
<p>
The <code>store_in</code> macro can also take lambdas - a common case for this
is multi-tenant applications.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Band</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 store_in database: ->{ <span class="constant">Thread</span>.current[<span class="symbol">:database</span>] }
<span class="keyword">end</span>
</pre></div>
</div>
<p>
When a document is stored in the database the ruby object will get
serialized into BSON and have a structure like so:
</p>
<div class="CodeRay">
<div class="code"><pre>{
 <span class="key"><span class="delimiter">"</span><span class="content">_id</span><span class="delimiter">"</span></span> : ObjectId(<span class="string"><span class="delimiter">"</span><span class="content">4d3ed089fb60ab534684b7e9</span><span class="delimiter">"</span></span>),
 <span class="key"><span class="delimiter">"</span><span class="content">title</span><span class="delimiter">"</span></span> : <span class="string"><span class="delimiter">"</span><span class="content">Sir</span><span class="delimiter">"</span></span>,
 <span class="key"><span class="delimiter">"</span><span class="content">name</span><span class="delimiter">"</span></span> : {
 <span class="key"><span class="delimiter">"</span><span class="content">_id</span><span class="delimiter">"</span></span> : ObjectId(<span class="string"><span class="delimiter">"</span><span class="content">4d3ed089fb60ab534684b7ff</span><span class="delimiter">"</span></span>),
 <span class="key"><span class="delimiter">"</span><span class="content">first_name</span><span class="delimiter">"</span></span> : <span class="string"><span class="delimiter">"</span><span class="content">Durran</span><span class="delimiter">"</span></span>
 },
 <span class="key"><span class="delimiter">"</span><span class="content">addresses</span><span class="delimiter">"</span></span> : [
 {
 <span class="key"><span class="delimiter">"</span><span class="content">_id</span><span class="delimiter">"</span></span> : ObjectId(<span class="string"><span class="delimiter">"</span><span class="content">4d3ed089fb60ab534684b7e0</span><span class="delimiter">"</span></span>),
 <span class="key"><span class="delimiter">"</span><span class="content">city</span><span class="delimiter">"</span></span> : <span class="string"><span class="delimiter">"</span><span class="content">Berlin</span><span class="delimiter">"</span></span>,
 <span class="key"><span class="delimiter">"</span><span class="content">country</span><span class="delimiter">"</span></span> : <span class="string"><span class="delimiter">"</span><span class="content">Deutschland</span><span class="delimiter">"</span></span>
 }
 ]
}
</pre></div>
</div>
</section>
<section id='fields'>
<h2>Fields</h2>
<p>
Even though MongoDB is a schemaless database, most uses will be with
web applications where form parameters always come to the server as strings.
Mongoid provides an easy mechanism for transforming these strings into
their appropriate types through the definition of fields in a
<code>Mongoid::Document</code>.
</p>
<p>
Consider a simple class for modeling a person in an application. A person may
have a first name, last name, and middle name. We can define these
attributes on a person by using the fields macro.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Person</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:first_name</span>, type: <span class="constant">String</span>
 field <span class="symbol">:middle_name</span>, type: <span class="constant">String</span>
 field <span class="symbol">:last_name</span>, type: <span class="constant">String</span>
<span class="keyword">end</span>
</pre></div>
</div>
<p>
Below is a list of valid types for fields.
<ul>
<li><code>Array</code></li>
<li><code>BigDecimal</code></li>
<li><code>Boolean</code></li>
<li><code>Date</code></li>
<li><code>DateTime</code></li>
<li><code>Float</code></li>
<li><code>Hash</code></li>
<li><code>Integer</code></li>
<li><code>Moped::BSON::ObjectId</code></li>
<li><code>Moped::BSON::Binary</code></li>
<li><code>Range</code></li>
<li><code>Regexp</code></li>
<li><code>String</code></li>
<li><code>Symbol</code></li>
<li><code>Time</code></li>
<li><code>TimeWithZone</code></li>
</ul>
</p>
<p>
If you decide not to specify the type of field with the definition, Mongoid
will treat it as an object and not try to typecast it when sending the
values to the database. This can be advantageous in 2 places, since
the lack of attempted conversion will yield a slight performance gain. However
some fields are not supported if not defined as fields. A note of thumb for
what fields you can use are:
<ul>
<li>You're not using a web front end and values are already properly cast.</li>
<li>All of your fields are strings.</li>
</ul>
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Person</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:first_name</span>
 field <span class="symbol">:middle_name</span>
 field <span class="symbol">:last_name</span>
<span class="keyword">end</span>
</pre></div>
</div>
<p>
Types that are not supported as dynamic attributes since they cannot be cast
are:
<ul>
<li><code>BigDecimal</code></li>
<li><code>Date</code></li>
<li><code>DateTime</code></li>
<li><code>Range</code></li>
</ul>
</p>
<a name='getters_and_setters'></a>
<h3>Getting and setting field values</h3>
<p>
When a field is defined, Mongoid provides several different ways of
accessing the field.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="comment"># Get the value of the first name field.</span>
person.first_name
person[<span class="symbol">:first_name</span>]
person.read_attribute(<span class="symbol">:first_name</span>)

<span class="comment"># Set the value for the first name field.</span>
person.first_name = <span class="string"><span class="delimiter">"</span><span class="content">Jean</span><span class="delimiter">"</span></span>
person[<span class="symbol">:first_name</span>] = <span class="string"><span class="delimiter">"</span><span class="content">Jean</span><span class="delimiter">"</span></span>
person.write_attribute(<span class="symbol">:first_name</span>, <span class="string"><span class="delimiter">"</span><span class="content">Jean</span><span class="delimiter">"</span></span>)
</pre></div>
</div>
<p>
In cases where you want to set multiple field values at once, there are a
few different ways of handling this as well.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="comment"># Get the field values as a hash.</span>
person.attributes

<span class="comment"># Set the field values in the document.</span>
<span class="constant">Person</span>.new(first_name: <span class="string"><span class="delimiter">"</span><span class="content">Jean-Baptiste</span><span class="delimiter">"</span></span>, middle_name: <span class="string"><span class="delimiter">"</span><span class="content">Emmanuel</span><span class="delimiter">"</span></span>)
person.attributes = { first_name: <span class="string"><span class="delimiter">"</span><span class="content">Jean-Baptiste</span><span class="delimiter">"</span></span>, middle_name: <span class="string"><span class="delimiter">"</span><span class="content">Emmanuel</span><span class="delimiter">"</span></span> }
person.write_attributes(
 first_name: <span class="string"><span class="delimiter">"</span><span class="content">Jean-Baptiste</span><span class="delimiter">"</span></span>,
 middle_name: <span class="string"><span class="delimiter">"</span><span class="content">Emmanuel</span><span class="delimiter">"</span></span>
)
</pre></div>
</div>
<a name='hash_field_keys'></a>
<h3>Hash Field Keys</h3>
<p>
When using a field of type <code>Hash</code>, be wary of adhering to the
<a href="http://www.mongodb.org/display/DOCS/Legal+Key+Names">legal key names for mongoDB</a>
, or else the values will not store properly.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Person</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:first_name</span>
 field <span class="symbol">:url</span>, type: <span class="constant">Hash</span>

 <span class="comment"># will update the fields properly and save the values</span>
 <span class="keyword">def</span> <span class="function">set_vals</span>
 <span class="predefined-constant">self</span>.first_name = <span class="string"><span class="delimiter">'</span><span class="content">Daniel</span><span class="delimiter">'</span></span>
 <span class="predefined-constant">self</span>.url = {<span class="string"><span class="delimiter">'</span><span class="content">home_page</span><span class="delimiter">'</span></span> => <span class="string"><span class="delimiter">'</span><span class="content">http://www.homepage.com</span><span class="delimiter">'</span></span>}
 save
 <span class="keyword">end</span>

 <span class="comment"># all data will fail to save due to the illegal hashkey</span>
 <span class="keyword">def</span> <span class="function">set_vals_fail</span>
 <span class="predefined-constant">self</span>.first_name = <span class="string"><span class="delimiter">'</span><span class="content">Daniel</span><span class="delimiter">'</span></span>
 <span class="predefined-constant">self</span>.url = {<span class="string"><span class="delimiter">'</span><span class="content">home.page</span><span class="delimiter">'</span></span> => <span class="string"><span class="delimiter">'</span><span class="content">http://www.homepage.com</span><span class="delimiter">'</span></span>}
 save
 <span class="keyword">end</span>
<span class="keyword">end</span>
</pre></div>
</div>
<a name='field_defaults'></a>
<h3>Defaults</h3>
<p>
You can tell a field in Mongoid to always have a default value if nothing
has been provided. Defaults are either static values or lambdas.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Person</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:blood_alcohol_level</span>, type: <span class="constant">Float</span>, default: <span class="float">0.40</span>
 field <span class="symbol">:last_drink</span>, type: <span class="constant">Time</span>, default: ->{ <span class="integer">10</span>.minutes.ago }
<span class="keyword">end</span>
</pre></div>
</div>
<p>
Be wary that default values that are not defined as lambdas or procs are
evaluated at class load time, so the following 2 definitions are not
equivalent. (You probably would prefer the second, which is at document
creation time.)
</p>
<div class="CodeRay">
<div class="code"><pre>field <span class="symbol">:dob</span>, type: <span class="constant">Time</span>, default: <span class="constant">Time</span>.now
field <span class="symbol">:dob</span>, type: <span class="constant">Time</span>, default: ->{ <span class="constant">Time</span>.now }
</pre></div>
</div>
<p>
If you want to set a default with a dependency on the document's state,
<code>self</code> inside a lambda or proc evaluates to the document instance.
</p>
<div class="CodeRay">
<div class="code"><pre>field <span class="symbol">:wasted_at</span>, type: <span class="constant">Time</span>, default: ->{ new_record? ? <span class="integer">2</span>.hours.ago : <span class="constant">Time</span>.now }
</pre></div>
</div>
<div class='well'>
<table>
<tr>
<td class='achtung'><img src="/images/achtung.png" /></td>
<td class='note'>
When defining a default value as a proc, Mongoid will apply the default
after all other attributes are set. If you want this to happen before
the other attributes, set <code>pre_processed: true</code>.
</td>
</tr>
</table>
</div>
<a name='field_aliasing'></a>
<h3>Field Aliasing</h3>
<p>
One of the drawbacks of having a schemaless database is that MongoDB must
store all field information along with every document, meaning that it takes
up a lot of storage space in RAM and on disk. A common pattern to limit this
is to alias fields to a small number of characters, while keeping the
domain in the application expressive. Mongoid allows you to do this and
reference the fields in the domain via their long names in getters, setters,
and criteria while performing the conversion for you.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Band</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:n</span>, as: <span class="symbol">:name</span>, type: <span class="constant">String</span>
<span class="keyword">end</span>

band = <span class="constant">Band</span>.new(name: <span class="string"><span class="delimiter">"</span><span class="content">Placebo</span><span class="delimiter">"</span></span>)
band.attributes <span class="comment">#=> { "n" => "Placebo" }</span>

criteria = <span class="constant">Band</span>.where(name: <span class="string"><span class="delimiter">"</span><span class="content">Placebo</span><span class="delimiter">"</span></span>)
criteria.selector <span class="comment">#=> { "n" => "Placebo" }</span>
</pre></div>
</div>
<a name='custom_fields'></a>
<h3>Custom field serialization</h3>
<p>
You can define custom types in Mongoid and determine how they are
serialized and deserialized. You simply need to provide 3 methods on it
for Mongoid to call to convert your object to and from MongoDB friendly
values.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Profile</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:location</span>, type: <span class="constant">Point</span>
<span class="keyword">end</span>

<span class="keyword">class</span> <span class="class">Point</span>

 attr_reader <span class="symbol">:x</span>, <span class="symbol">:y</span>

 <span class="keyword">def</span> <span class="function">initialize</span>(x, y)
 <span class="instance-variable">@x</span>, <span class="instance-variable">@y</span> = x, y
 <span class="keyword">end</span>

 <span class="comment"># Converts an object of this instance into a database friendly value.</span>
 <span class="keyword">def</span> <span class="function">mongoize</span>
 [ x, y ]
 <span class="keyword">end</span>

 <span class="keyword">class</span> << <span class="class">self</span>

 <span class="comment"># Get the object as it was stored in the database, and instantiate</span>
 <span class="comment"># this custom class from it.</span>
 <span class="keyword">def</span> <span class="function">demongoize</span>(object)
 <span class="constant">Point</span>.new(object[<span class="integer">0</span>], object[<span class="integer">1</span>])
 <span class="keyword">end</span>

 <span class="comment"># Takes any possible object and converts it to how it would be</span>
 <span class="comment"># stored in the database.</span>
 <span class="keyword">def</span> <span class="function">mongoize</span>(object)
 <span class="keyword">case</span> object
 <span class="keyword">when</span> <span class="constant">Point</span> <span class="keyword">then</span> object.mongoize
 <span class="keyword">when</span> <span class="constant">Hash</span> <span class="keyword">then</span> <span class="constant">Point</span>.new(object[<span class="symbol">:x</span>], object[<span class="symbol">:y</span>]).mongoize
 <span class="keyword">else</span> object
 <span class="keyword">end</span>
 <span class="keyword">end</span>

 <span class="comment"># Converts the object that was supplied to a criteria and converts it</span>
 <span class="comment"># into a database friendly form.</span>
 <span class="keyword">def</span> <span class="function">evolve</span>(object)
 <span class="keyword">case</span> object
 <span class="keyword">when</span> <span class="constant">Point</span> <span class="keyword">then</span> object.mongoize
 <span class="keyword">else</span> object
 <span class="keyword">end</span>
 <span class="keyword">end</span>
 <span class="keyword">end</span>
<span class="keyword">end</span>
</pre></div>
</div>
<p>
The instance method <code>mongoize</code> take an instance of your object,
and converts it into how it will be stored in the database. In our example
above, we want to store our point object as an array in the form
<code>[ x, y ]</code>.
</p>
<p>
The class method <code>demongoize</code> takes an object as how it was
stored in the database, and is responsible for instantiating an object of
your custom type. In this case, we take an array and instantiate a
<code>Point</code> from it.
</p>
<p>
The class method <code>mongoize</code> takes an object that you would use
to set on your model from your application code, and create the object as
it would be stored in the database. This is for cases where you are not
passing your model instances of your custom type in the setter:
</p>
<div class="CodeRay">
<div class="code"><pre>point = <span class="constant">Point</span>.new(<span class="integer">12</span>, <span class="integer">24</span>)
venue = <span class="constant">Venue</span>.new(location: point) <span class="comment">#=> This uses the mongoize instance method.</span>
venue = <span class="constant">Venue</span>.new(location: [ <span class="integer">12</span>, <span class="integer">24</span> ]) <span class="comment">#=> This uses the mongoize class method.</span>
</pre></div>
</div>
<p>
The class method <code>evolve</code> takes an object, and determines how it
is to be transformed for use in criteria. For example we may want to write
a query like so:
</p>
<div class="CodeRay">
<div class="code"><pre>point = <span class="constant">Point</span>.new(<span class="integer">12</span>, <span class="integer">24</span>)
<span class="constant">Venue</span>.where(location: point)
</pre></div>
</div>
<div class='well'>
<table>
<tr>
<td class='achtung'><img src="/images/achtung.png" /></td>
<td class='note'>
When accessing custom fields from the document, you will get a new
instance of that object with each call to the getter. This is
because Mongoid is generating a new object from the object from the
raw attributes on each access.
</td>
</tr>
</table>
</div>
<p>
We need the point object in the criteria to be transformed to a Mongo
friendly value when it is not as well, and <code>evolve</code> is the method
that takes care of this. We check if the passed in object is a <code>Point</code>
first, in case we also want to be able to pass in ordinary arrays instead.
</p>
<a name='reserved_field_names'></a>
<h3>Reserved names</h3>
<p>
If you define a field on your document that conflicts with a reserved method
name in Mongoid, the configuration will raise an error. For a list of these
you may look at <code>Mongoid.destructive_fields</code>.
</p>
<a name='custom_ids'></a>
<h3>Creating Custom Ids</h3>
<p>
For cases when you do not want to have <code>Moped::BSON::ObjectId</code> ids, you
can override Mongoid's <code>_id</code> field and set them to whatever you
like.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Band</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:name</span>, type: <span class="constant">String</span>
 field <span class="symbol">:_id</span>, type: <span class="constant">String</span>, default: ->{ name }
<span class="keyword">end</span>
</pre></div>
</div>
</section>
<section id='dynamic_fields'>
<h2>Dynamic fields</h2>
<p>
By default Mongoid supports dynamic fields - that is it will allow
attributes to get set and persisted on the document even if a field was not
defined for them. There is a slight 'gotcha' however when dealing with
dynamic attributes in that Mongoid is not completely lenient about the use of
<code>method_missing</code> and breaking the public interface of the Document
class.
</p>
<p>
When dealing with dynamic attributes the following rules apply:
</p>
<p>
If the attribute exists in the document, Mongoid will provide you with your
standard getter and setter methods. For example, consider a person who has an
attribute of "gender" set on the document:
</p>
<div class="CodeRay">
<div class="code"><pre><span class="comment"># Set the person's gender to male.</span>
person[<span class="symbol">:gender</span>] = <span class="string"><span class="delimiter">"</span><span class="content">Male</span><span class="delimiter">"</span></span>
person.gender = <span class="string"><span class="delimiter">"</span><span class="content">Male</span><span class="delimiter">"</span></span>

<span class="comment"># Get the person's gender.</span>
person.gender
</pre></div>
</div>
<p>
If the attribute does not already exist on the document, Mongoid will not
provide you with the getters and setters and will enforce normal
<code>method_missing</code> behavior. In this case you must use the other
provided accessor methods: (<code>[]</code> and <code>[]=</code>) or
(<code>read_attribute</code> and <code>write_attribute</code>).
</p>
<div class="CodeRay">
<div class="code"><pre><span class="comment"># Raise a NoMethodError if value isn't set.</span>
person.gender
person.gender = <span class="string"><span class="delimiter">"</span><span class="content">Male</span><span class="delimiter">"</span></span>

<span class="comment"># Retrieve a dynamic field safely.</span>
person[<span class="symbol">:gender</span>]
person.read_attribute(<span class="symbol">:gender</span>)

<span class="comment"># Write a dynamic field safely.</span>
person[<span class="symbol">:gender</span>] = <span class="string"><span class="delimiter">"</span><span class="content">Male</span><span class="delimiter">"</span></span>
person.write_attribute(<span class="symbol">:gender</span>, <span class="string"><span class="delimiter">"</span><span class="content">Male</span><span class="delimiter">"</span></span>)
</pre></div>
</div>
<p>
Dynamic attributes can be completely turned off by setting the Mongoid
configuration option <code>allow_dynamic_fields</code> to <code>false</code>.
</p>
</section>
<section id='localized_fields'>
<h2>Localized fields</h2>
<p>
From 2.4.0 Mongoid now supports localized fields without the need of any
external gem.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Product</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:description</span>, localize: <span class="predefined-constant">true</span>
<span class="keyword">end</span>
</pre></div>
</div>
<p>
By telling the field to <code>localize</code>, Mongoid will under the covers
store the field as a hash of locale/value pairs, but normal access to it will
behave like a string.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="comment"># Using a default locale of en.</span>
product = <span class="constant">Product</span>.new
product.description = <span class="string"><span class="delimiter">"</span><span class="content">Marvelous!</span><span class="delimiter">"</span></span>
<span class="constant">I18n</span>.locale = <span class="symbol">:de</span>
product.description = <span class="string"><span class="delimiter">"</span><span class="content">Fantastisch!</span><span class="delimiter">"</span></span>

product.attributes
<span class="comment">#=> { "description" => { "en" => "Marvelous!", "de" => "Fantastisch!" }</span>
</pre></div>
</div>
<p>
You can get and set all the translations at once by using the
corresponding <code>_translations</code> method.
</p>
<div class="CodeRay">
<div class="code"><pre>product.description_translations
<span class="comment">#=> { "description" => { "en" => "Marvelous!", "de" => "Fantastisch!" }</span>
product.description_translations =
 { <span class="string"><span class="delimiter">"</span><span class="content">description</span><span class="delimiter">"</span></span> => { <span class="string"><span class="delimiter">"</span><span class="content">en</span><span class="delimiter">"</span></span> => <span class="string"><span class="delimiter">"</span><span class="content">Marvelous!</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">de</span><span class="delimiter">"</span></span> => <span class="string"><span class="delimiter">"</span><span class="content">Wunderbar!</span><span class="delimiter">"</span></span> }
</pre></div>
</div>
<h3>Fallbacks</h3>
<p>
When using fallbacks, Mongoid will automatically use them when a translation
is not available.
</p>
<p>
For Rails applications, set the fallbacks configuration setting to true in
your environment.
</p>
<div class="CodeRay">
<div class="code"><pre>config.i18n.fallbacks = <span class="predefined-constant">true</span>
</pre></div>
</div>
<p>
For non-rails applications, you must include the fallbacks module straight
to the I18n gem.
</p>
<div class="CodeRay">
<div class="code"><pre>require <span class="string"><span class="delimiter">"</span><span class="content">i18n/backend/fallbacks</span><span class="delimiter">"</span></span>
<span class="constant">I18n</span>::<span class="constant">Backend</span>::<span class="constant">Simple</span>.send(<span class="symbol">:include</span>, <span class="constant">I18n</span>::<span class="constant">Backend</span>::<span class="constant">Fallbacks</span>)
</pre></div>
</div>
<p>
Then when the fallbacks are defined, if a translation is not present Mongoid
will fallback in order of the defined locales.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="comment"># Using a default locale of en.</span>
::<span class="constant">I18n</span>.fallbacks[<span class="symbol">:de</span>] = [ <span class="symbol">:de</span>, <span class="symbol">:en</span>, <span class="symbol">:es</span> ]
product = <span class="constant">Product</span>.new
product.description = <span class="string"><span class="delimiter">"</span><span class="content">Marvelous!</span><span class="delimiter">"</span></span>
<span class="constant">I18n</span>.locale = <span class="symbol">:de</span>
product.description <span class="comment">#=> "Marvelous!"</span>
</pre></div>
</div>
<h3>Querying</h3>
<p>
When querying for localized fields using Mongoid's criteria API, Mongoid
will automatically alter the criteria to match the current locale.
</p>
<mongoid>mongoid</mongoid>
<div class="CodeRay">
<div class="code"><pre><span class="comment"># Match all prodcucts with Marvelous as the description. Locale is en.</span>
<span class="constant">Product</span>.where(description: <span class="string"><span class="delimiter">"</span><span class="content">Marvelous!</span><span class="delimiter">"</span></span>)
</pre></div>
</div>
<mongodb>mongodb query selector</mongodb>
<div class="CodeRay">
<div class="code"><pre>{ <span class="string"><span class="delimiter">"</span><span class="content">description.en</span><span class="delimiter">"</span></span> : <span class="string"><span class="delimiter">"</span><span class="content">Marvelous!</span><span class="delimiter">"</span></span> }
</pre></div>
</div>
<h3>Indexing</h3>
<p>
If you plan to be querying extensively on localized fields, you should index
each of the locales that you plan on searching on.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Product</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:description</span>, localize: <span class="predefined-constant">true</span>

 index <span class="string"><span class="delimiter">"</span><span class="content">description.de</span><span class="delimiter">"</span></span> => <span class="integer">1</span>
 index <span class="string"><span class="delimiter">"</span><span class="content">description.en</span><span class="delimiter">"</span></span> => <span class="integer">1</span>
<span class="keyword">end</span>
</pre></div>
</div>
<h3>Validation</h3>
<p>
Mongoid's presence validator will make sure that translations are present
for all locales that are in the underlying hash.
</p>
</section>
<section id='dirty'>
<h2>Dirty Tracking</h2>
<p>
Mongoid supports tracking of changed or "dirty" fields with an API that
mirrors that of Active Model. If a defined field has been modified in
a model the model will be marked as dirty and some additional behavior
comes into play.
</p>
<h3>Viewing changes</h3>
<p>
There are various ways to view what has been altered on a model. Changes
are recorded from the time a document is instantiated, either as a
new document or via loading from the database up to the time it is
saved. Any persistence operation clears the changes.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Person</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:name</span>, type: <span class="constant">String</span>
<span class="keyword">end</span>

person = <span class="constant">Person</span>.first
person.name = <span class="string"><span class="delimiter">"</span><span class="content">Alan Garner</span><span class="delimiter">"</span></span>

<span class="comment"># Check to see if the document has changed.</span>
person.changed? <span class="comment">#=> true</span>

<span class="comment"># Get an array of the names of the changed fields.</span>
person.changed <span class="comment">#=> [ :name ]</span>

<span class="comment"># Get a hash of the old and changed values for each field.</span>
person.changes <span class="comment">#=> { "name" => [ "Alan Parsons", "Alan Garner" ] }</span>

<span class="comment"># Check if a specific field has changed.</span>
person.name_changed? <span class="comment">#=> true</span>

<span class="comment"># Get the changes for a specific field.</span>
person.name_change <span class="comment">#=> [ "Alan Parsons", "Alan Garner" ]</span>

<span class="comment"># Get the previous value for a field.</span>
person.name_was <span class="comment">#=> "Alan Parsons"</span>
</pre></div>
</div>
<h3>Resetting changes</h3>
<p>
You can reset changes of a field to its previous value by calling the
reset method.
</p>
<div class="CodeRay">
<div class="code"><pre>person = <span class="constant">Person</span>.first
person.name = <span class="string"><span class="delimiter">"</span><span class="content">Alan Garner</span><span class="delimiter">"</span></span>

<span class="comment"># Reset the changed name back to the original</span>
person.reset_name!
person.name <span class="comment">#=> "Alan Parsons"</span>
</pre></div>
</div>
<h3>Notes on persistence</h3>
<p>
Mongoid uses dirty tracking as the core of its persistence operations.
It looks at the changes on a document and atomically updates only what
has changed unlike other frameworks that write the entire document on
each save. If no changes have been made, Mongoid will not hit the database
on a call to <code>Model#save</code>.
</p>
<h3>Viewing previous changes</h3>
<p>
After a document has been persisted, you can see what the changes were
previously by calling <code>Model#previous_changes</code>
</p>
<div class="CodeRay">
<div class="code"><pre>person = <span class="constant">Person</span>.first
person.name = <span class="string"><span class="delimiter">"</span><span class="content">Alan Garner</span><span class="delimiter">"</span></span>
person.save <span class="comment">#=> Clears out current changes.</span>

<span class="comment"># View the previous changes.</span>
person.previous_changes <span class="comment">#=> { "name" => [ "Alan Parsons", "Alan Garner" ] }</span>
</pre></div>
</div>
</section>
<section id='security'>
<h2>Security</h2>
<p>
There are cases where you don't want Mongoid to allow attributes to be
set through mass assignment, like passwords. This is a common event when
submitting forms, and fields can be protected by using Mongoid's
<code>attr_protected</code> or <code>attr_accessible</code> thanks to the
wonders of Active Model.
</p>
<div class='well'>
<table>
<tr>
<td class='achtung'><img src="/images/achtung.png" /></td>
<td class='note'>
Mongoid auto-protects the <code>_id</code> and <code>_type</code>
attributes by default.
</td>
</tr>
</table>
</div>
<h3>Protected</h3>
<p>
When defining a list of fields as protected, all other fields in the
document will NOT be able to be set through mass assignment.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">User</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:first_name</span>, type: <span class="constant">String</span>
 field <span class="symbol">:password</span>, type: <span class="constant">String</span>
 attr_protected <span class="symbol">:password</span>
<span class="keyword">end</span>

<span class="comment"># Set attributes on a person properly.</span>
<span class="constant">Person</span>.new(first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span>)
person.attributes = { first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span> }
person.write_attributes(first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span>)

<span class="comment"># Attempt to set attributes a person, logging an error.</span>
<span class="constant">Person</span>.new(first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span>, password: <span class="string"><span class="delimiter">"</span><span class="content">password</span><span class="delimiter">"</span></span>)
person.attributes = { first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span>, password: <span class="string"><span class="delimiter">"</span><span class="content">password</span><span class="delimiter">"</span></span> }
person.write_attributes(first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span>, password: <span class="string"><span class="delimiter">"</span><span class="content">password</span><span class="delimiter">"</span></span>)
</pre></div>
</div>
<h3>Accessible</h3>
<p>
Providing a list of fields as accessible is simply the inverse of
protecting them. Anything not defined as accessible will cause the
error.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">User</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:first_name</span>, type: <span class="constant">String</span>
 field <span class="symbol">:password</span>, type: <span class="constant">String</span>
 attr_accessible <span class="symbol">:first_name</span>
<span class="keyword">end</span>

<span class="comment"># Set attributes on a user properly.</span>
<span class="constant">User</span>.new(first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span>)
user.attributes = { first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span> }
user.write_attributes(first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span>)

<span class="comment"># Attempt to set attributes on a user, will silently ignore protected ones.</span>
<span class="constant">User</span>.new(first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span>, password: <span class="string"><span class="delimiter">"</span><span class="content">password</span><span class="delimiter">"</span></span>)
user.attributes = { first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span>, password: <span class="string"><span class="delimiter">"</span><span class="content">password</span><span class="delimiter">"</span></span> }
user.write_attributes(first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span>, password: <span class="string"><span class="delimiter">"</span><span class="content">password</span><span class="delimiter">"</span></span>)
</pre></div>
</div>
<p>
You can scope the mass assignment by role by providing the role as an
option to the constructor or create methods.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">User</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:first_name</span>, type: <span class="constant">String</span>
 field <span class="symbol">:password</span>, type: <span class="constant">String</span>
 attr_accessible <span class="symbol">:first_name</span>, as: [ <span class="symbol">:default</span>, <span class="symbol">:admin</span> ]
<span class="keyword">end</span>

<span class="comment"># Set attributes on a person for the admin role</span>
<span class="constant">Person</span>.new({ first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span> }, as: <span class="symbol">:admin</span>)
<span class="constant">Person</span>.create({ first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span> }, as: <span class="symbol">:default</span>)
<span class="constant">Person</span>.create!({ first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span> }, as: <span class="symbol">:admin</span>)
</pre></div>
</div>
<h3>Overriding</h3>
<p>
In the case you want to override the security in a single call,
you can pass a block to the document constructor to set fields manually.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="constant">Person</span>.new(first_name: <span class="string"><span class="delimiter">"</span><span class="content">Corbin</span><span class="delimiter">"</span></span>) <span class="keyword">do</span> |person|
 person.password = <span class="string"><span class="delimiter">"</span><span class="content">password</span><span class="delimiter">"</span></span>
<span class="keyword">end</span>
</pre></div>
</div>
</section>
<section id='readonly'>
<h2>Readonly Attributes</h2>
<p>
You can tell Mongoid that certain attributes are readonly. This will
allow documents to be created with theses attributes, but changes to
them will be filtered out.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Band</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:name</span>, type: <span class="constant">String</span>
 field <span class="symbol">:origin</span>, type: <span class="constant">String</span>

 attr_readonly <span class="symbol">:name</span>, <span class="symbol">:origin</span>
<span class="keyword">end</span>

band = <span class="constant">Band</span>.create(name: <span class="string"><span class="delimiter">"</span><span class="content">Placebo</span><span class="delimiter">"</span></span>)
band.update_attributes(name: <span class="string"><span class="delimiter">"</span><span class="content">Tool</span><span class="delimiter">"</span></span>) <span class="comment"># Filters out the name change.</span>
</pre></div>
</div>
<p>
If you explicitly try to update or remove the attribute by itself, then
a <code>ReadonlyAttribute</code> error will be raised.
</p>
<div class="CodeRay">
<div class="code"><pre>band.update_attribute(<span class="symbol">:name</span>, <span class="string"><span class="delimiter">"</span><span class="content">Tool</span><span class="delimiter">"</span></span>) <span class="comment"># Raises the error.</span>
band.remove_attribute(<span class="symbol">:name</span>) <span class="comment"># Raises the error.</span>
</pre></div>
</div>
</section>
<section id='inheritance'>
<h2>Inheritance</h2>
<p>
Mongoid supports inheritance in both root and embedded documents. In
scenarios where documents are inherited from their fields, relations,
validations and scopes get copied down into their child documents, but
not vise-versa.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="keyword">class</span> <span class="class">Canvas</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:name</span>, type: <span class="constant">String</span>
 embeds_many <span class="symbol">:shapes</span>
<span class="keyword">end</span>

<span class="keyword">class</span> <span class="class">Browser</span> < <span class="constant">Canvas</span>
 field <span class="symbol">:version</span>, type: <span class="constant">Integer</span>
 scope <span class="symbol">:recent</span>, where(<span class="symbol">:version</span>.gt => <span class="integer">3</span>)
<span class="keyword">end</span>

<span class="keyword">class</span> <span class="class">Firefox</span> < <span class="constant">Browser</span>
<span class="keyword">end</span>

<span class="keyword">class</span> <span class="class">Shape</span>
 include <span class="constant">Mongoid</span>::<span class="constant">Document</span>
 field <span class="symbol">:x</span>, type: <span class="constant">Integer</span>
 field <span class="symbol">:y</span>, type: <span class="constant">Integer</span>
 embedded_in <span class="symbol">:canvas</span>
<span class="keyword">end</span>

<span class="keyword">class</span> <span class="class">Circle</span> < <span class="constant">Shape</span>
 field <span class="symbol">:radius</span>, type: <span class="constant">Float</span>
<span class="keyword">end</span>

<span class="keyword">class</span> <span class="class">Rectangle</span> < <span class="constant">Shape</span>
 field <span class="symbol">:width</span>, type: <span class="constant">Float</span>
 field <span class="symbol">:height</span>, type: <span class="constant">Float</span>
<span class="keyword">end</span>
</pre></div>
</div>
<p>
In the above example, <code>Canvas</code>, <code>Browser</code> and
<code>Firefox</code> will all save in the <code>canvases</code> collection.
An additional attribute <code>_type</code> is stored in order
to make sure when loaded from the database the correct document is
returned. This also holds true for the embedded documents <code>Circle</code>,
<code>Rectangle</code>, and <code>Shape</code>.
</p>
<h3>Querying for Subclasses</h3>
<p>
Querying for subclasses is handled in the normal manner, and although
the documents are all in the same collection, queries will only return
documents of the correct type, similar to Single Table Inheritance in
ActiveRecord.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="comment"># Returns Canvas documents and subclasses</span>
<span class="constant">Canvas</span>.where(name: <span class="string"><span class="delimiter">"</span><span class="content">Paper</span><span class="delimiter">"</span></span>)
<span class="comment"># Returns only Firefox documents</span>
<span class="constant">Firefox</span>.where(name: <span class="string"><span class="delimiter">"</span><span class="content">Window 1</span><span class="delimiter">"</span></span>)
</pre></div>
</div>
<h3>Associations</h3>
<p>
You can add any type of subclass to a has one or has many association,
through either normal setting or through the build and create methods
on the association:
</p>
<div class="CodeRay">
<div class="code"><pre>firefox = <span class="constant">Firefox</span>.new
<span class="comment"># Builds a Shape object</span>
firefox.shapes.build({ x: <span class="integer">0</span>, y: <span class="integer">0</span> })
<span class="comment"># Builds a Circle object</span>
firefox.shapes.build({ x: <span class="integer">0</span>, y: <span class="integer">0</span> }, <span class="constant">Circle</span>)
<span class="comment"># Creates a Rectangle object</span>
firefox.shapes.create({ x: <span class="integer">0</span>, y: <span class="integer">0</span> }, <span class="constant">Rectangle</span>)

rect = <span class="constant">Rectangle</span>.new(width: <span class="integer">100</span>, height: <span class="integer">200</span>)
firefox.shapes</pre></div>
</div>
</section>
</div>
</div>
</body>
</html>