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

Nested eager loading fails with nils

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • 6.0.0, 5.1.5
    • Affects Version/s: 5.1.4
    • Component/s: None

      Mongoid raises an unexpected NoMethodError when trying to eager load through a nested relationship that is `nil`.

      Concretely, given the models

      class Blog
        include Mongoid::Document
        has_one :post
      end
      
      class Post
        include Mongoid::Document
        belongs_to :blog
        belongs_to :author
      end
      
      class Author
        include Mongoid::Document
      end
      

      with seed data

      Blog.create()
      

      the following code

      Blog.includes(:post => :author).to_a
      

      raises the error

      NoMethodError: undefined method `set_relation' for nil:NilClass
      from /Users/martin/.rvm/gems/ruby-2.2.3/gems/mongoid-5.1.4/lib/mongoid/relations/eager/base.rb:148:in `set_relation'
      

      Note that the non-nested version works correctly:

      Blog.includes(:post).to_a
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            mmun Martin Muñoz
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: