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

Specs to ensure that unknown fields in an embedded document + allow_dynamic_fields=false should not == exception

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

      When you have allow_dynamic_fields set to false and in your database you have a field that isn't declared inside of an embedded field, initializing the embedded document will raise a NoMethodError exception. This is not the case if the unknown field is on the root document. In the following example, the Location class has a state_or_province field but the database already has a document with a state field.

      class Location
      include Mongoid::Document
      embedded_in :bar
      field :city, :type => String
      field :state_or_province, :type => String
      end

      class Bar
      include Mongoid::Document
      field :name, :type => String
      embeds_one :location
      end

      Mongoid.database['bars'].insert(:name => "Cheers", :location => { :city => "Boston", :state => "Massachusetts"})
      Bar.first #=> #<Bar _id: 4da4f4a4d4ec9e695b000001, name: "Cheers", _id: BSON::ObjectId('4da4f4a4d4ec9e695b000001'), _type: nil>
      Bar.first.location #=> NoMethodError: undefined method `state=' for #<Location:0x10b298c70>

            Assignee:
            Unassigned Unassigned
            Reporter:
            cgriego Chris Griego
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: