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

Using insert_one stops Mongoid from being able to update the record

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Ruby Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      I've run into a weird issue where using insert_one to insert a record works, but then that record can't be updated in Mongo. base_salary in this case is a BigDecimal field. Note how the following base_salary field shows as updates after the set command, but is not being persisted.

       

      attrs = Employee.new(base_salary: "123")
      Employee.collection.insert_one(**attrs)
      
      e = Employee.last
      
      puts e.base_salary.to_s
      => "123.0"
      puts e.read_attribute_before_type_cast(:base_salary)
      => "123"
      
      e.set(base_salary: "124")
      
      puts e.base_salary.to_s
      => "124.0"
      puts e.read_attribute_before_type_cast(:base_salary)
      => "124"
      
      e.reload
      
      puts e.base_salary.to_s
      => "123.0"
      puts e.read_attribute_before_type_cast(:base_salary)
      => "123"
      
      Employee.collection.update_one(
        { _id: e.id },
        { '$set': { base_salary: "124" } },
      )
      
      e.reload
      
      puts e.base_salary.to_s
      => "123.0"
      puts e.read_attribute_before_type_cast(:base_salary)
      => "123"
      
      e = Employee.last
      
      puts e.base_salary.to_s
      => "123.0"
      puts e.read_attribute_before_type_cast(:base_salary)
      => "123" 

       

       

      I'm really struggling to figure out why this would be. Anyone got any ideas?

            Assignee:
            dmitry.rybakov@mongodb.com Dmitry Rybakov
            Reporter:
            kieran@xpand.io Kieran Pilkington
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: