File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -408,3 +408,42 @@ If you want the functionality of neither document being persisted in Mongoid 7 o
408
408
and earlier, the ``validate: true`` option can be set on the association. If
409
409
you want the functionality of only the Child being persisted, the ``validate:
410
410
false`` option can be set on the association.
411
+
412
+
413
+ Update Local HABTM Association Correctly
414
+ ----------------------------------------
415
+
416
+ In Mongoid 8, when pushing persisted elements to a HABTM association, the
417
+ association will now update correctly without requiring a reload.
418
+ For example:
419
+
420
+ .. code::
421
+
422
+ class User
423
+ include Mongoid::Document
424
+ has_and_belongs_to_many :posts
425
+ end
426
+
427
+ class Post
428
+ include Mongoid::Document
429
+ has_and_belongs_to_many :users
430
+ end
431
+
432
+ user1 = User.create!
433
+ user2 = User.create!
434
+
435
+ post = user1.posts.create!
436
+
437
+ p post.users.length
438
+ # => 1
439
+
440
+ post.users << user2
441
+
442
+ p post.users.length
443
+ # => 1 in Mongoid 7, 2 in Mongoid 8
444
+
445
+ p post.reload.users.length
446
+ # => 2
447
+
448
+ As you can see from this example, after pushing ``user2`` to the users array,
449
+ Mongoid 8 correctly updates the number of elements without requiring a reload.
You can’t perform that action at this time.
0 commit comments