Navigation

Mongoid 7.5

This page describes significant changes and improvements in Mongoid 7.5. The complete list of releases is available on GitHub and in JIRA; please consult GitHub releases for detailed release notes and JIRA for the complete list of issues fixed in each release, including bug fixes.

Order of Callbacks Invocation

Breaking change: Mongoid 7.5 changes order of _create and _save callbacks invocation for documents with associations.

Referenced associations (has_one and has_many):

Mongoid 7.5 Mongoid 7.4
Parent :before_save Parent :before_save
Parent :around_save_open Parent :around_save_open
Parent :before_create Parent :before_create
Parent :around_create_open Parent :around_create_open
Parent persisted in MongoDB Parent persisted in MongoDB
Child :before_save Parent :around_create_close
Child :around_save_open Parent :after_create
Child :before_create Child :before_save
Child :around_create_open Child :around_save_open
  Child :before_create
  Child :around_create_open
Child persisted in MongoDB Child persisted in MongoDB
Child :around_create_close Child :around_create_close
Child :after_create Child :after_create
Child :around_save_close Child :around_save_close
Child :after_save Child :after_save
Parent :around_create_close Parent :around_save_close
Parent :after_create Parent :after_save
Parent :around_save_close  
Parent :after_save  

Embedded associations (embeds_one and embeds_many):

Mongoid 7.5 Mongoid 7.4
Parent :before_save Child :before_save
Parent :around_save_open Child :around_save_open
Parent :before_create Child :around_save_close
Parent :around_create_open Child :after_save
Child :before_save Parent :before_save
Child :around_save_open Parent :around_save_open
Child :before_create Child :before_create
Child :around_create_open Child :around_create_open
  Child :around_create_close
  Child :after_create
  Parent :before_create
  Parent :around_create_open
Document persisted in MongoDB Document persisted in MongoDB
Child :around_create_close  
Child :after_create  
Child :around_save_close  
Child :after_save  
Parent :around_create_close Parent :around_create_close
Parent :after_create Parent :after_create
Parent :around_save_close Parent :around_save_close
Parent :after_save Parent :after_save

Changeable Module Behavior Made Compatible With ActiveModel::Ditry

When updating documents, it is now possible to get updated attribute values in after_* callbacks. This is following with ActiveRecord/ActiveModel behavior.

class Cat
  include Mongoid::Document

  field :age, type: Integer

  after_save do
      p self
      p attribute_was(:age)
  end
end

a = Cat.create!
a.age = 2
a.save!

Mongoid 7.5 output:

#<Cat _id: 60aef1652c97a617438dc9bb, age: 2>
2

Mongoid 7.4 output:

#<Cat _id: 60aef1652c97a617438dc9bb, age: 2>
nil

Notice that in 7.4 attribute_was(:age) returns an old attribute value, while in 7.5 attribute_was(:age) returns the new values, .. |branch| replace:: master .. |copy| unicode:: U+000A9 .. |year| replace:: 2021 .. |hardlink| replace:: http://docs.mongodb.com/mongoid

←   Release Notes Mongoid 7.4  →