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

update_all for embeds_many errors out if there are no embedded documents

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

      Using Mongoid master, 9aa7e2975877:

      class Book
        include Mongoid::Document
        embeds_many :pages
        field :title
      end
      class Page
        include Mongoid::Document
        embedded_in :book
        field :text
      end
      
      b = Book.create(:title => "Cloud Atlas")
      b.pages.update_all(:text => "Sonmi")
      
      # NoMethodError: undefined method `find' for nil:NilClass
      #   from /Users/jonathan/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-9aa7e2975877/lib/mongoid/contextual/memory.rb:269:in `update_documents'
      #   from /Users/jonathan/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-9aa7e2975877/lib/mongoid/contextual/memory.rb:245:in `update_all'
      #   from /Users/jonathan/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-9aa7e2975877/lib/mongoid/contextual.rb:19:in `update_all'
      #   from /Users/jonathan/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-9aa7e2975877/lib/mongoid/relations/embedded/many.rb:412:in `block in method_missing'
      #   from /Users/jonathan/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-9aa7e2975877/lib/mongoid/scoping.rb:190:in `with_scope'
      #   from /Users/jonathan/.rvm/gems/ruby-1.9.3-p194/bundler/gems/mongoid-9aa7e2975877/lib/mongoid/relations/embedded/many.rb:411:in `method_missing'
      #   from (irb):13
      #   from /Users/jonathan/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
      #   from /Users/jonathan/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
      #   from /Users/jonathan/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
      #   from script/rails:6:in `require'
      #   from script/rails:6:in `<main>'
      
      b.pages.create()
      # => #<Page _id: 5060daebdd365a2c52000002, text: nil> 
      
      b.pages.update_all(:text => "Sonmi")
      #  => {"updatedExisting"=>true, "n"=>1, "connectionId"=>89, "err"=>nil, "ok"=>1.0} 
      

            Assignee:
            durran Durran Jordan
            Reporter:
            jonhyman Jon Hyman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: