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

Nested attributes do not set the correct _type on inherited classes

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.2
    • Affects Version/s: None
    • Component/s: None
    • None

      We use Mongoid v3.1.0

      We have:

      class Canvas
      ...
      include Mongoid::Document
      embeds_many :shapes, class_name: 'CanvasShapes::BaseShape'
      accepts_nested_attributes_for :shapes, allow_destroy: true
      ...
      end

      module CanvasShapes
      class BaseShape
      include Mongoid::Document
      ...
      embedded_in :canvas
      ...
      end
      end

      module CanvasShapes
      class Circle < BaseShape
      ...
      end
      end

      module CanvasShapes
      class Triangle < BaseShape
      ...
      end
      end

      module CanvasShapes
      class Rectangle < BaseShape
      ...
      end
      end

      And when I passed next params:

      canvas_attributes: {
      "foo": {
      shapes_attributes: {
      "foo":

      { _type: "CanvasShapes::Triangle" ... }

      }
      }
      }

      mongoid ignores the '_type' attribute and saves Triangle as a Shape.
      I've found the temporary fix: if in any place (for example in controller) an instance of CanvasShapes::Triangle was created (CanvasShapes::Triangle.new), it set type correctly.

      I have found https://github.com/mongoid/mongoid/pull/1030, and it seems to the problem had to be solved, but maybe something was missed.

            Assignee:
            durran.jordan@mongodb.com Durran Jordan
            Reporter:
            mayatskiy mayatskiy
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: