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

Serializing documents retrieved from the database: include doesn't work for all elements in a embeds_many relation

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

      Hi!

      I have the following structure in my application:

      Tree embeds_many Leaves
      Leaf embeds_one Worm

      And I had problems with serialization.

      I create a Tree document with 2 Leaves. Each one of those Leaves contains one Worm.
      The, I try to convert Tree to Json with to_json and excluding all the _id fields for Tree, Leaves and Worms.

      Tree.to_json(:except => :_id,
      :include => { :leaves => {:except => :_id,
      :include => { :worm => {:except => :_id}
      }
      }
      })

      If I serialize Tree before storing it in the database everything works as expected. Cool! The problem is when I retrieve the Tree document from the database (Tree.find(whatever)) and try to serialize it. What I get there is that only the first Worm is serialized without the _id field.

      I've created a spec that reproduces the failure.

      I've fixed the problem by changing how serialize_relations works. Before the change, it deletes the :inclusion key form the serializable options. This works ok for everything except when we have an embeds_many with an embeds_something, because only the first document of the embeds_many knows that it needs to include something (That's why the second worm does not know anything about removing the _id field).

      Am I doing something wrong retrieving the Tree document from the database? Is this fix correct? (All the specs are green after the change, of course )

      Thank you!

            Assignee:
            Unassigned Unassigned
            Reporter:
            plagelao plagelao
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: