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

Error when appending new embedded documents on already saved parent (non standard structure)

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

      Given this class definitition (i know its not the easiest but beside this problem it works pretty fine):

      class Parent
        include Mongoid::Document
        include Mongoid::Timestamps
        
        embeds_many :children, :class_name => '::Parent::Child'
        
        class Child
          include Mongoid::Document
          
          identity :type => String
          field :active, :type => Boolean, :default => true
          
          embedded_in :parent, :class_name => '::Parent'
        end
      end
      

      I can append new Children to the Parent ONLY for not yet saved Parents:

      p = Parent.new
      p.children << Parent::Child.new
      p.save
      

      the following does not work:

      p = Parent.last
      p.children << Parent::Child.new
      

      and gives me this exception:

      NoMethodError: undefined method `name' for nil:NilClass
      	from ..gems-path.../ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/whiny_nil.rb:48:in `method_missing'
      	from ..gems-path.../ruby-1.9.2-p180/gems/mongoid-2.0.1/lib/mongoid/paths.rb:38:in `_position'
      	from ..gems-path.../ruby-1.9.2-p180/gems/mongoid-2.0.1/lib/mongoid/persistence/insert_embedded.rb:34:in `persist'
      	from ..gems-path.../ruby-1.9.2-p180/gems/mongoid-2.0.1/lib/mongoid/persistence/insert.rb:43:in `insert'
      	from ..gems-path.../ruby-1.9.2-p180/gems/mongoid-2.0.1/lib/mongoid/persistence/insert.rb:30:in `block (2 levels) in persist'
      	from ..gems-path.../ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:414:in `_run_create_callbacks'
      	from ..gems-path.../ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:94:in `run_callbacks'
      	from ..gems-path.../ruby-1.9.2-p180/gems/mongoid-2.0.1/lib/mongoid/persistence/insert.rb:29:in `block in persist'
      	from ..gems-path.../ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:414:in `_run_save_callbacks'
      	from ..gems-path.../ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:94:in `run_callbacks'
      	from ..gems-path.../ruby-1.9.2-p180/gems/mongoid-2.0.1/lib/mongoid/persistence/insert.rb:28:in `persist'
      	from ..gems-path.../ruby-1.9.2-p180/gems/mongoid-2.0.1/lib/mongoid/persistence.rb:47:in `insert'
      	from ..gems-path.../ruby-1.9.2-p180/gems/mongoid-2.0.1/lib/mongoid/persistence.rb:148:in `upsert'
      	from ..gems-path.../ruby-1.9.2-p180/gems/mongoid-2.0.1/lib/mongoid/relations/many.rb:27:in `block in <<'
      	from ..gems-path.../ruby-1.9.2-p180/gems/mongoid-2.0.1/lib/mongoid/relations/many.rb:24:in `each'
      	from ..gems-path.../ruby-1.9.2-p180/gems/mongoid-2.0.1/lib/mongoid/relations/many.rb:24:in `<<'
      

      I already tried to figure out why this is happening but i dont see any relationship between the obvious new_record? in the _position method and metadata.name that actually causes the error

            Assignee:
            durran Durran Jordan
            Reporter:
            maxigs maxigs
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: