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

Nested eager loading fails with nils

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical - P2
    • Resolution: Fixed
    • Affects Version/s: 5.1.4
    • Fix Version/s: 6.0.0, 5.1.5
    • Component/s: None
    • Labels:

      Description

      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
      

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: