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

Rename child#to_a

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

      I had written a custom to_a method on an embedded document, without realizing that Mongoid uses to_a internally and expects it to work a certain way.

      In this instance:

      module Mongoid #:nodoc
        module Hierarchy #:nodoc
          module InstanceMethods #:nodoc:
      
            # @return [ Array<Document> ] All child documents in the hierarchy.
            def _children
              @_children ||=
                [].tap do |children|
                  relations.each_pair do |name, metadata|
                    if metadata.embedded?
                      child = send(name)
                      child.to_a.each do |doc|
                        children.push(doc)
                        children.concat(doc._children) unless metadata.versioned?
                      end if child
                    end
                  end
                end
            end
      

      When child.to_a is called, it appears to expect a document to wrap itself in an array (e.g. calling to_a on #<SomeDocument> should return <SomeDocument>).

      If a document's to_a method returns any elements that can't respond to _children, Mongoid will raise an exception. Given that it's not that uncommon to write a custom to_a, wouldn't it be better to use a different method here?

            Assignee:
            Unassigned Unassigned
            Reporter:
            Aupajo Pete Nicholls [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: