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

_type must be a string

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 7.0.0, 6.3.0
    • Affects Version/s: 6.2.1
    • Component/s: None
    • None

      Let us consider an example with two models, where Doctor inherits from Person.

      class Person
        include Mongoid::Document
        field :name
        embeds_many :locations, class_name: 'Location'
      end
      
      class Location
        include Mongoid::Document
        field :name
        embedded_in :person
      end
      class Street < Location
        field :number
      end
      

      When a new person is instanced with many streets:

      Person.new(name: 'Peter', locations: [{_type: 'Street', number: 5}])
      

      Returns:

      Mongoid::Errors::UnknownAttribute: 
      message:
        Attempted to set a value for 'number' which is not allowed on the model Street.
      summary:
        Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call Street#number= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError.
      resolution:
        You can include Mongoid::Attributes::Dynamic if you expect to be writing values for undefined fields often.
      

      Since _type is a symbol it is completely ignored, it must be a String. What is the point of that? Shouldn't it be indifferent?

      Problem is in lib/mongoid/factory.rb

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

              Created:
              Updated:
              Resolved: