Skip to content

Commit d4bbd40

Browse files
MONGOID-5217 Add _previously_was methods (#5195)
1 parent e38f323 commit d4bbd40

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

source/release-notes/mongoid-8.0.txt

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ Mongoid 8.0 behavior:
169169

170170
Band.any_of({name: 'Rolling Stone'}, {founded: 1990}).
171171
any_of({members: 2}, {last_tour: 1995})
172-
# =>
172+
# =>
173173
# #<Mongoid::Criteria
174174
# selector: {"$or"=>[{"name"=>"Rolling Stone"}, {"founded"=>1990}],
175175
# "$and"=>[{"$or"=>[{"members"=>2}, {"last_tour"=>1995}]}]}
@@ -178,7 +178,7 @@ Mongoid 8.0 behavior:
178178
# embedded: false>
179179

180180
Band.any_of({name: 'Rolling Stone'}, {founded: 1990}).any_of({members: 2})
181-
# =>
181+
# =>
182182
# #<Mongoid::Criteria
183183
# selector: {"$or"=>[{"name"=>"Rolling Stone"}, {"founded"=>1990}], "members"=>2}
184184
# options: {}
@@ -191,7 +191,7 @@ Mongoid 7 behavior:
191191

192192
Band.any_of({name: 'Rolling Stone'}, {founded: 1990}).
193193
any_of({members: 2}, {last_tour: 1995})
194-
# =>
194+
# =>
195195
# #<Mongoid::Criteria
196196
# selector: {"$or"=>[{"name"=>"Rolling Stone"}, {"founded"=>1990},
197197
# {"members"=>2}, {"last_tour"=>1995}]}
@@ -200,7 +200,7 @@ Mongoid 7 behavior:
200200
# embedded: false>
201201

202202
Band.any_of({name: 'Rolling Stone'}, {founded: 1990}).any_of({members: 2})
203-
# =>
203+
# =>
204204
# #<Mongoid::Criteria
205205
# selector: {"$or"=>[{"name"=>"Rolling Stone"}, {"founded"=>1990}], "members"=>2}
206206
# options: {}
@@ -248,3 +248,31 @@ Mongoid 7 output:
248248

249249
Notice that in 7 ``attribute_was(:age)`` returns the old attribute value,
250250
while in 8.0 ``attribute_was(:age)`` returns the new value.
251+
252+
``*_previously_was``, ``previously_new_record?``, and ``previously_persisted?`` helpers
253+
---------------------------------------------------------------------------------------
254+
255+
Mongoid 8.0 introduces ActiveModel-compatible ``*_previously_was`` helpers,
256+
as well as ActiveRecord-compatible ``previously_new_record?`` and
257+
``previously_persisted?`` helpers:
258+
259+
.. code-block:: ruby
260+
261+
class User
262+
include Mongoid::Document
263+
264+
field :name, type: String
265+
field :age, type: Integer
266+
end
267+
268+
user = User.create!(name: 'Sam', age: 18)
269+
user.previously_new_record? # => true
270+
271+
user.name = "Nick"
272+
user.save!
273+
user.name_previously_was # => "Sam"
274+
user.age_previously_was # => 18
275+
user.previously_new_record? # => false
276+
277+
user.destroy
278+
user.previously_persisted? # => true

0 commit comments

Comments
 (0)