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

add_to_set does not work with array of hashes/embedded documents

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

      Add_to_set does not work with array of hashes/embedded documents.
      For example:

      Model.create().add_to_set(data: [{a: 1}])
      

      gives:

      NoMethodError: undefined method `metadata' for {:a => 1}:Hash
      

      and

      Model.create().add_to_set(data: [EmbeddedModel.new])
      

      gives:

      NoMethodError: undefined method `__bson_dump__' for #<EmbeddedModel:>
      

      workaround:

      m = Model.create()
      Model.collection.find(_id: m._id).update_all("$addToSet" => { data: {'$each' => [{a:1},{b:2}]} })
      

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            jurriaan Jurriaan Pruis
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: