Skip to content

Commit f8e91d0

Browse files
MONGOID-5104 Add AR-style dirty attributes (#5092)
Dirty data are now cleared and changes moved previous_changes immediately after a document is persisted in MongoDB. It means that old attribute values are available in before_ callbacks, and updated attribute values are available in after_ callbacks.
1 parent a5e1f0c commit f8e91d0

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

source/release-notes/mongoid-7.5.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,46 @@ Embedded associations (``embeds_one`` and ``embeds_many``):
117117
+---------------------------------------+---------------------------------------+
118118
| Parent :after_save | Parent :after_save |
119119
+---------------------------------------+---------------------------------------+
120+
121+
122+
``Changeable`` Module Behavior Made Compatible With ``ActiveModel::Ditry``
123+
--------------------------------------------------------------------------
124+
125+
When updating documents, it is now possible to get updated attribute values
126+
in ``after_*`` callbacks. This is following with ActiveRecord/ActiveModel
127+
behavior.
128+
129+
.. code-block:: ruby
130+
131+
class Cat
132+
include Mongoid::Document
133+
134+
field :age, type: Integer
135+
136+
after_save do
137+
p self
138+
p attribute_was(:age)
139+
end
140+
end
141+
142+
a = Cat.create!
143+
a.age = 2
144+
a.save!
145+
146+
Mongoid 7.5 output:
147+
148+
.. code-block:: ruby
149+
150+
#<Cat _id: 60aef1652c97a617438dc9bb, age: 2>
151+
2
152+
153+
154+
Mongoid 7.4 output:
155+
156+
.. code-block:: ruby
157+
158+
#<Cat _id: 60aef1652c97a617438dc9bb, age: 2>
159+
nil
160+
161+
Notice that in 7.4 ``attribute_was(:age)`` returns an old attribute value,
162+
while in 7.5 ``attribute_was(:age)`` returns the new values,

0 commit comments

Comments
 (0)