mongoid/mongoid — Issue #1580: Fields of type Array cannot be assigned nil after their value is set
Author: dblock
Created: 2012-01-10 17:38:05+00:00
State: open

```
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'
```