Setting an embeds_many association to empty array before save does not persist it

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Unknown
    • 9.1.1
    • Affects Version/s: 8.0.10, 8.1.10
    • Component/s: None
    • None
    • Fully Compatible
    • Ruby Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      The following tests used to pass in MONGOID_VERSION=7.5.4

      require 'bundler/inline'
      require 'logger'
      
      gemfile do
        source "https://rubygems.org"
      
        gem "mongoid", "#{ENV["MONGOID_VERSION"] || '8.0.10'}"
        gem "debug", "~> 1.7"
        gem "rspec", require: "rspec/autorun"
      end
      
      require 'mongoid'
      
      Mongoid.configure do |config|
        config.clients.default = {
          hosts: ['localhost:27017'],
          database: 'mongoid8_test',
        }
        config.log_level = :debug
      end
      
      puts Mongoid::VERSION
      
      
      class Band
        include Mongoid::Document
        include Mongoid::Attributes::Dynamic
      
        field :name, type: String
        embeds_many :labels
      end
      
      class Label
        include Mongoid::Document
        include Mongoid::Timestamps::Updated::Short
      
        field :name, type: String
      
        embedded_in :band
      end
      
      Band.delete_all
      Label.delete_all
      
      describe Band do
      
        context "when trying to persist the empty list" do
      
          context "in an embeds_many relation" do
      
            # let(:band) { Band.create } # this seems to work
            let(:band) { Band.new }
      
            before do
              band.labels = []
              band.save!
            end
      
            let(:reloaded_band) { Band.collection.find(_id: band._id).first }
      
            it "persists the empty list" do
              expect(reloaded_band).to have_key(:labels)
              expect(reloaded_band[:labels]).to eq []
            end
          end
      
          context "using before_save callback" do
      
            let(:band) { Band.create }
      
            before do
              Band.before_save do
                self[:labels] ||= []
              end
            end
      
            let(:reloaded_band) { Band.collection.find(_id: band._id).first }
      
            it "persists the empty list" do
              expect(reloaded_band).to have_key(:labels)
              expect(reloaded_band[:labels]).to eq []
            end
          end
        end
      end
      

      Might be related to https://jira.mongodb.org/browse/MONGOID-4869 

            Assignee:
            Jamis Buck
            Reporter:
            Joe Lim
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: