@@ -41,17 +41,17 @@ operations with examples.
41
41
42
42
*Insert a document or multiple documents into the database, raising an
43
43
error if a validation or server error occurs.*
44
-
44
+
45
45
*Pass a hash of attributes to create one document with the specified
46
46
attributes, or an array of hashes to create multiple documents.
47
47
If a single hash is passed, the corresponding document is returned.
48
48
If an array of hashes is passed, an array of documents corresponding
49
49
to the hashes is returned.*
50
-
50
+
51
51
*If a block is given to* ``create!`` *, it will be invoked with each
52
52
document as the argument in turn prior to attempting to save that
53
53
document.*
54
-
54
+
55
55
*If there is a problem saving any of the documents, such as
56
56
a validation error or a server error, an exception is raised
57
57
and, consequently, none of the documents are returned.
@@ -78,12 +78,12 @@ operations with examples.
78
78
79
79
*Instantiate a document or multiple documents and, if validations pass,
80
80
insert them into the database.*
81
-
81
+
82
82
``create`` *is similar to* ``create!`` *but does not raise
83
83
exceptions on validation errors. It still raises errors on server
84
84
errors, such as trying to insert a document with an* ``_id`` *that
85
85
already exists in the collection.*
86
-
86
+
87
87
*If any validation errors are encountered, the respective document
88
88
is not inserted but is returned along with documents that were inserted.
89
89
Use* ``persisted?`` *,* ``new_record?`` *or* ``errors`` *methods
@@ -105,13 +105,13 @@ operations with examples.
105
105
Person.create(first_name: "Heinrich") do |doc|
106
106
doc.last_name = "Heine"
107
107
end # => Person instance
108
-
108
+
109
109
class Post
110
110
include Mongoid::Document
111
-
111
+
112
112
validates_uniqueness_of :title
113
113
end
114
-
114
+
115
115
posts = Post.create([{title: "test"}, {title: "test"}])
116
116
# => array of two Post instances
117
117
posts.map { |post| post.persisted? } # => [true, false]
@@ -120,7 +120,7 @@ operations with examples.
120
120
121
121
*Save the changed attributes to the database atomically, or insert the document if
122
122
new. Raises an exception if validations fail or there is a server error.*
123
-
123
+
124
124
*Returns true if the changed attributes were saved, raises an exception otherwise.*
125
125
-
126
126
.. code-block:: ruby
@@ -138,12 +138,12 @@ operations with examples.
138
138
139
139
*Save the changed attributes to the database atomically, or insert the document
140
140
if new.*
141
-
141
+
142
142
*Returns true if the changed attributes were saved. Returns false
143
143
if there were any validation errors. Raises an exception if
144
144
the document passed validation but there was a server error during
145
145
the save.*
146
-
146
+
147
147
*Pass* ``validate: false`` *option to bypass validations.*
148
148
-
149
149
.. code-block:: ruby
@@ -210,7 +210,7 @@ operations with examples.
210
210
provided time field. This will cascade the touch to all*
211
211
``belongs_to`` *associations of the document with the option set.
212
212
This operation skips validations and callbacks.*
213
-
213
+
214
214
*Attempting to touch a destroyed document will raise* ``FrozenError``
215
215
* (as of Ruby 2.5,* ``RuntimeError`` *on previous Ruby versions),
216
216
same as if attempting to update an attribute on a destroyed
@@ -224,14 +224,14 @@ operations with examples.
224
224
* - ``Model#delete``
225
225
226
226
*Deletes the document from the database without running callbacks.*
227
-
227
+
228
228
*If the document is not persisted, Mongoid will attempt to delete from
229
229
the database any document with the same* ``_id``.
230
230
-
231
231
.. code-block:: ruby
232
232
233
233
person.delete
234
-
234
+
235
235
person = Person.create!(...)
236
236
unsaved_person = Person.new(id: person.id)
237
237
unsaved_person.delete
@@ -241,14 +241,14 @@ operations with examples.
241
241
* - ``Model#destroy``
242
242
243
243
*Deletes the document from the database while running destroy callbacks.*
244
-
244
+
245
245
*If the document is not persisted, Mongoid will attempt to delete from
246
246
the database any document with the same* ``_id``.
247
247
-
248
248
.. code-block:: ruby
249
249
250
250
person.destroy
251
-
251
+
252
252
person = Person.create!(...)
253
253
unsaved_person = Person.new(id: person.id)
254
254
unsaved_person.destroy
@@ -399,9 +399,9 @@ are not invoked.
399
399
*Updates an attribute on the model instance and, if the instance
400
400
is already persisted, performs an atomic $set on the field, bypassing
401
401
validations.*
402
-
402
+
403
403
``set`` *can also deeply set values on Hash fields.*
404
-
404
+
405
405
``set`` *can also deeply set values on* ``embeds_one`` *associations.
406
406
If such an association's document is nil, one will be created prior
407
407
to the update.*
@@ -424,14 +424,14 @@ are not invoked.
424
424
425
425
class Post
426
426
include Mongoid::Document
427
-
427
+
428
428
field :metadata, type: Hash
429
429
end
430
-
430
+
431
431
post = Post.create!
432
432
post.set('metadata.published_at' => Time.now)
433
433
post.metadata['published_at'] # => Time instance
434
-
434
+
435
435
post.set('metadata.approved.today' => true)
436
436
post.metadata['approved'] # => {'today' => true}
437
437
@@ -449,7 +449,7 @@ are not invoked.
449
449
450
450
field :route, type: String
451
451
end
452
-
452
+
453
453
flight = Flight.create!
454
454
flight.plan # => nil
455
455
flight.set('plan.route', 'test route')
@@ -578,18 +578,18 @@ associations would be loaded from the database at the next access.
578
578
does not make any changes to the document:
579
579
580
580
.. code-block:: ruby
581
-
581
+
582
582
# Assuming band has many tours, which could be referenced:
583
583
band = Band.create!(tours: [Tour.create!])
584
584
# ... or embedded:
585
585
band = Band.create!(tours: [Tour.new])
586
-
586
+
587
587
# This writes the empty tour list into the database.
588
588
band.tours = []
589
-
589
+
590
590
# There are no unsaved modifications in band at this point to be reverted.
591
591
band.reload
592
-
592
+
593
593
# Returns the empty array since this is what is in the database.
594
594
band.tours
595
595
# => []
@@ -612,7 +612,7 @@ example demonstrates:
612
612
613
613
Mongoid.raise_not_found_error = false
614
614
band.destroy
615
-
615
+
616
616
band.reload
617
617
# => #<Band _id: 6206d031e1b8324561f179c8, name: nil, description: nil, likes: nil>
618
618
@@ -630,7 +630,7 @@ specified in the document (and the shard key value, if a shard key is defined):
630
630
.. code-block:: ruby
631
631
632
632
existing = Band.create!(name: 'Photek')
633
-
633
+
634
634
# Unsaved document
635
635
band = Band.new(id: existing.id)
636
636
band.reload
@@ -665,9 +665,9 @@ each declared field:
665
665
666
666
field :first_name
667
667
end
668
-
668
+
669
669
person = Person.new
670
-
670
+
671
671
person.first_name = "Artem"
672
672
person.first_name
673
673
# => "Artem"
@@ -693,18 +693,18 @@ write the values directly into the attributes hash:
693
693
def first_name
694
694
read_attribute(:fn)
695
695
end
696
-
696
+
697
697
def first_name=(value)
698
698
write_attribute(:fn, value)
699
699
end
700
700
end
701
-
701
+
702
702
person = Person.new
703
-
703
+
704
704
person.first_name = "Artem"
705
705
person.first_name
706
706
# => "Artem"
707
-
707
+
708
708
person.attributes
709
709
# => {"_id"=>BSON::ObjectId('606477dc2c97a628cf47075b'), "fn"=>"Artem"}
710
710
@@ -727,7 +727,7 @@ accept either the declared field name or the storage field name for operations:
727
727
field :first_name, as: :fn
728
728
field :last_name, as: :ln
729
729
end
730
-
730
+
731
731
person = Person.new(first_name: "Artem")
732
732
# => #<Person _id: 60647a522c97a6292c195b4b, first_name(fn): "Artem", last_name(ln): nil>
733
733
@@ -736,11 +736,11 @@ accept either the declared field name or the storage field name for operations:
736
736
737
737
person.read_attribute(:fn)
738
738
# => "Artem"
739
-
739
+
740
740
person.write_attribute(:last_name, "Pushkin")
741
741
person
742
742
# => #<Person _id: 60647a522c97a6292c195b4b, first_name(fn): "Artem", last_name(ln): "Pushkin">
743
-
743
+
744
744
person.write_attribute(:ln, "Medvedev")
745
745
person
746
746
# => #<Person _id: 60647a522c97a6292c195b4b, first_name(fn): "Artem", last_name(ln): "Medvedev">
@@ -756,7 +756,7 @@ does not cause the respective field to be defined either:
756
756
# => #<Person _id: 60647b212c97a6292c195b4c, first_name(fn): "Artem", last_name(ln): "Medvedev">
757
757
person.attributes
758
758
# => {"_id"=>BSON::ObjectId('60647b212c97a6292c195b4c'), "first_name"=>"Artem", "last_name"=>"Medvedev", "undefined"=>"Hello"}
759
-
759
+
760
760
person.read_attribute(:undefined)
761
761
# => "Hello"
762
762
person.undefined
@@ -784,17 +784,17 @@ for the detailed description of their behavior.
784
784
end
785
785
786
786
person = Person.new(first_name: "Artem")
787
-
787
+
788
788
person["fn"]
789
789
# => "Artem"
790
-
790
+
791
791
person[:first_name]
792
792
# => "Artem"
793
-
793
+
794
794
person[:ln] = "Medvedev"
795
795
person
796
796
# => #<Person _id: 606483742c97a629bdde5cfc, first_name(fn): "Artem", last_name(ln): "Medvedev">
797
-
797
+
798
798
person["last_name"] = "Pushkin"
799
799
person
800
800
# => #<Person _id: 606483742c97a629bdde5cfc, first_name(fn): "Artem", last_name(ln): "Pushkin">
@@ -863,6 +863,14 @@ the database up to the time it is saved. Any persistence operation clears the ch
863
863
# Get the previous value for a field.
864
864
person.name_was # "Alan Parsons"
865
865
866
+ .. note::
867
+
868
+ Setting the associations on a document does not cause the ``changes`` or
869
+ ``changed_attributes`` hashes to be modified. This is true for all associations
870
+ whether referenced or embedded. Note that changing the _id(s) field on
871
+ referenced associations does cause the changes to show up in the ``changes``
872
+ and the ``changed_attributes`` hashes.
873
+
866
874
867
875
Resetting Changes
868
876
-----------------
0 commit comments