Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1233

Can't access related model on before_destroy

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.2.1
    • Affects Version/s: None
    • Component/s: None

      Inside the before_destroy method of a model I can't seem to access a related record.

      I've made this Gist to demonstrate the issue, upon running it I get:

       
      ruby test-mongo.rb
      test-mongo.rb:24:in `print_parent_name': undefined method `name' for nil:NilClass (NoMethodError)
          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'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:31:in `destroy'
      	from /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'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/relations/cascading/destroy.rb:14:in `each'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/relations/cascading/destroy.rb:14:in `cascade'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/relations/cascading.rb:31:in `block in cascade!'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/relations/cascading.rb:28:in `each'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/relations/cascading.rb:28:in `cascade!'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence/deletion.rb:25:in `prepare'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence/operations/remove.rb:26:in `persist'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:56:in `remove'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:31:in `block in destroy'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:390:in `_run_destroy_callbacks'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
      	from /Users/rafa/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.2.0/lib/mongoid/persistence.rb:31:in `destroy'
      	from test-mongo.rb:35:in `<main>'
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            escobera escobera
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: