-
Type: Bug
-
Resolution: Fixed
-
Priority: Minor - P4
-
Affects Version/s: None
-
Component/s: Associations
https://docs.mongodb.com/mongoid/master/tutorials/mongoid-relations/#dependent-behavior specifies that the default dependent behavior is to nullify. However, in this case:
class C include Mongoid::Document belongs_to :b end class B include Mongoid::Document has_many :cs, class_name: "C", inverse_of: :b end b=B.create! c=C.create!(b:b) b.destroy c.reload # => #<C _id: 5c5f7b9e026d7c4248d44e27, b_id: BSON::ObjectId('5c5f7b9e026d7c4248d44e26')>
... c still retained a b_id referencing the now nonexistent b.
If nullify is manually specified the behavior is correct:
class C include Mongoid::Document belongs_to :b end class B include Mongoid::Document has_many :cs, class_name: "C", inverse_of: :b, dependent: :nullify end b=B.create! c=C.create!(b:b) b.destroy c.reload # => #<C _id: 5c5f7bde026d7c43881dc718, b_id: nil>
If ActiveRecord's default behavior is nullify as well, Mongoid's behavior should be changed to nullify to match both our existing documentation and AR's behavior.
From ActiveRecord's documentation: https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Dependent+associations
"When no option is given, the behavior is to do nothing with the associated records when destroying a record."
This seems to be consistent with how we handle the situation... Perhaps we should update the documentation to reflect this?
- related to
-
MONGOID-4707 dependent does not cascade
- Backlog