Created
August 4, 2011 09:24
-
-
Save dbackeus/1124824 to your computer and use it in GitHub Desktop.
Disappeared parent in Mongoid after_destroy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Parent | |
| include Mongoid::Document | |
| embeds_many :children | |
| before_create :create_children | |
| before_destroy :destroy_children | |
| private | |
| def create_children | |
| 3.times { children.build } | |
| end | |
| def destroy_children | |
| children.destroy_all | |
| end | |
| end | |
| class Child | |
| include Mongoid::Document | |
| embedded_in :parent | |
| before_destroy :do_something_related_to_parent | |
| private | |
| def do_something_related_to_parent | |
| parent.children # ERROR: undefined method `children' for nil:NilClass | |
| end | |
| end | |
| Parent.create.destroy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Having the same issue with Mongoid 2.1.5. Have you found the issue with this? In my case before_destroy was ok. But parent returned nil when its called from after_destroy callback.