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

Inserting into Set does not work as expected

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 6.2.1
    • Component/s: None
    • None
    • Environment:
      Mongoid driver 6.2.1, Ruby 2.5.1

      When creating a Mongoid::Document the double left operator "<<" works as expected for field type of Array, however for field type of Set, it returns the correct value from the operation but does not update the instance attribute.

      Below is an example of the bug in a console.

      Here you can see the << operator returns the correctly appended "third" value to the set field but when queried the instance's attribute has not been set as expected.

      > class Foo
      >   include Mongoid::Document
      >   field :queue, :type => :array, default: Array.new
      >   field :bad_queue, :type => :set, default: Set.new
      >   end
       => #<Mongoid::Fields::Standard:0x00007f89557c39c8 @name="bad_queue", @options={:type=>Set, :default=>#<Set: {}>, :klass=>Foo}, @label=nil, @default_val=#<Set: {}>, @pre_processed=true> 
      > my_foo = Foo.new
       => #<Foo _id: 5c33cef8199fcd4ae792539e, queue: [], bad_queue: []> 
      
      > my_foo.queue = ["first", "second"]
       => ["first", "second"] 
      > my_foo.queue << "third"
       => ["first", "second", "third"] 
      > my_foo.queue
       => ["first", "second", "third"] 
      
      > my_foo.bad_queue = ["first", "second"]
       => ["first", "second"] 
      > my_foo.bad_queue << "third"
       => #<Set: {"first", "second", "third"}> 
      > my_foo.bad_queue
       => #<Set: {"first", "second"}> 
      

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            genkilabs Alex
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: