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

Fields of type Array cannot be assigned nil after their value is set

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

      require "spec_helper"
      
      class Foo
        include Mongoid::Document
        field :range, type: Array
      end
      
      describe "Mongoid::Document" do
      
        before do
          [ Foo ].each(&:delete_all)
        end
        
        describe "field" do
        
          let(:foo) do
            Foo.new
          end
          
          it "saves the relationship" do
            foo.range = [1000.0, 1000.0]
            foo.save! # works
            foo.range = nil
            foo.save! # fails
          end
          
        end
        
      end
      

      You get an unhelpful error.

           Failure/Error: foo.save!
           NoMethodError:
             undefined method `-' for nil:NilClass
           # ./lib/mongoid/dirty.rb:120:in `block (2 levels) in setters'
           # ./lib/mongoid/dirty.rb:114:in `each_pair'
           # ./lib/mongoid/dirty.rb:114:in `block in setters'
           # ./lib/mongoid/dirty.rb:113:in `tap'
           # ./lib/mongoid/dirty.rb:113:in `setters'
      

      Repro on 2.4.0-stable.

            Assignee:
            Unassigned Unassigned
            Reporter:
            dblock Daniel Doubrovkine
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: