{"issue":{"created_at":"2011-09-08T05:48:19Z","state":"closed","updated_at":"2011-12-01T16:57:47Z","comments":5,"user":{"gravatar_id":"fca9f7376b4527353138bf920817d59e","id":9344,"url":"https://api.github.com/users/escobera","avatar_url":"https://secure.gravatar.com/avatar/fca9f7376b4527353138bf920817d59e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"escobera"},"body":"Inside the before_destroy method of a model I can't seem to access a related record.\r\n\r\nI've made this [Gist](https://gist.github.com/1202714) to demonstrate the issue, upon running it I get: \r\n\r\n``` \r\nruby test-mongo.rb\r\ntest-mongo.rb:24:in `print_parent_name': undefined method `name' for nil:NilClass (NoMethodError)\r\n    from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:395:in `_run_destroy_callbacks'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:31:in `destroy'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/relations/cascading/destroy.rb:14:in `block in cascade'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/relations/cascading/destroy.rb:14:in `each'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/relations/cascading/destroy.rb:14:in `cascade'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/relations/cascading.rb:31:in `block in cascade!'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/relations/cascading.rb:28:in `each'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/relations/cascading.rb:28:in `cascade!'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence/deletion.rb:25:in `prepare'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/remove.rb:26:in `persist'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:56:in `remove'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:31:in `block in destroy'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:390:in `_run_destroy_callbacks'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks'\r\n\tfrom /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:31:in `destroy'\r\n\tfrom test-mongo.rb:35:in `<main>'\r\n```\r\n","assignee":null,"id":1595851,"labels":[{"name":"bug","color":"e10c02","url":"https://api.github.com/repos/mongoid/mongoid/labels/bug"},{"name":"confirmed","color":"e10c02","url":"https://api.github.com/repos/mongoid/mongoid/labels/confirmed"}],"number":1232,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"title":"Can't access related model on before_destroy","html_url":"https://github.com/mongoid/mongoid/issues/1232","closed_at":"2011-09-18T10:27:12Z","url":"https://api.github.com/repos/mongoid/mongoid/issues/1232","milestone":{"created_at":"2011-08-18T15:20:24Z","state":"closed","closed_issues":26,"due_on":"2011-09-22T07:00:00Z","creator":{"gravatar_id":"6f09262a13427532461b0b618d445b1e","id":9030,"url":"https://api.github.com/users/durran","avatar_url":"https://secure.gravatar.com/avatar/6f09262a13427532461b0b618d445b1e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"durran"},"number":20,"description":"","title":"2.2.1","url":"https://api.github.com/repos/mongoid/mongoid/milestones/20","open_issues":0}},"action":"created","comment":{"created_at":"2011-12-01T16:57:46Z","updated_at":"2011-12-01T16:57:46Z","user":{"gravatar_id":"5e7fec0787bde963028837524ef71421","id":951821,"url":"https://api.github.com/users/fullbridge-batkins","avatar_url":"https://secure.gravatar.com/avatar/5e7fec0787bde963028837524ef71421?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"fullbridge-batkins"},"body":"Using mongoid 2.2.4 on a rails project, ran into 2 different scenarios where :dependent => :destroy didn't work.\r\n\r\n    class Archetype\r\n       include Mongoid::Document\r\n       include Mongoid::Timestamps\r\n       include Mongoid::Versioning\r\n       include Mongoid::Paranoia\r\n       extend ImprovedManyToMany\r\n       cache\r\n       ...\r\n    end\r\n\r\n    class Section < Archetype\r\n      has_many :pods, :as => :parent, :dependent => :destroy\r\n      ...\r\n    end\r\n\r\n    class Pod < Archetype\r\n       belongs_to :parent, :polymorphic => true\r\n       has_one :search_field, :dependent => :destroy\r\n    end\r\n\r\nUsing the rails console, first grabbed a 'section' object, mapped out the (child) 'pod' id's beneath it.  Destroyed the section object but the pods all still existed in the database.  Then mapped out each of those pod's search_field objects (has_one relationship), and then proceeded to manually delete each 'pod' object.  Each of the associated search_objects still existed in the database and had to manually deleted.\r\n\r\nNot sure if this is happening due to the polymorphism, but makes me think that this bug might not be fixed for all scenario's?  Workaround is to do something like this on your class:\r\n\r\n    class Section < Archetype\r\n       has_many :pods, :as => :parent, :dependent => :destroy\r\n       before_destroy {pods.destroy_all} # to ensure the associated pods actually get destroyed when destroying a section\r\n    end\r\n\r\nWould appreciate it if someone looked into this, as I examined the code in the repo for cascading destroy (https://github.com/mongoid/mongoid/blob/master/lib/mongoid/relations/cascading/destroy.rb), and can see that it's only been touched once, in response to this issue, and was included in version 2.2.1 and higher, so that means that the version I'm running on (2.2.4) has this patch, but didn't work as expected in these scenarios.  Thanks!","id":2977132,"url":"https://api.github.com/repos/mongoid/mongoid/issues/comments/2977132"}}