mongoid/mongoid — Issue #1620: Array fields in embedded docs still not work properly
Author: Mik-die
Created: 2012-01-21 07:56:07+00:00
State: open

My previous issue https://github.com/mongoid/mongoid/issues/1587

Now I have troubles with dublicated values.

So my specs

```
require "spec_helper"

class Human
include Mongoid::Document
has_one :brain
end

class Brain
include Mongoid::Document
field :thoughts, :type => Array
belongs_to :human
end

describe Brain do
let!(:human){ Human.create }
let!(:brain){ human.create_brain :thoughts => [1,2,2,2] }

it "replaces first of dublicated element" do
brain.update_attributes :thoughts => [1,3,2,2]
brain.reload.thoughts.should == [1,3,2,2]
end

it "replaces second of dublicated element" do
brain.update_attributes :thoughts => [1,2,3,2]
brain.reload.thoughts.should == [1,2,3,2]
end

it "sets first element the same as other elements" do
brain.update_attributes :thoughts => [2,2,2,2]
brain.reload.thoughts.should == [2,2,2,2]
end

it "increases size of array" do
brain.update_attributes :thoughts => [1,2,2,2,2]
brain.reload.thoughts.should == [1,2,2,2,2]
end

it "decreases size of array" do
brain.update_attributes :thoughts => [1,2,2]
brain.reload.thoughts.should == [1,2,2]
end
end
```

result

```
FFFFF

Failures:

1) Brain replaces first of dublicated element
Failure/Error: brain.reload.thoughts.should == [1,3,2,2]
expected: [1, 3, 2, 2]
got: [1, 2, 2, 2, 3] (using ==)
Diff:
@@ -1,2 +1,2 @@
-[1, 3, 2, 2]
+[1, 2, 2, 2, 3]
# ./spec/models/human_spec.rb:20:in `block (2 levels) in '

2) Brain replaces second of dublicated element
Failure/Error: brain.reload.thoughts.should == [1,2,3,2]
expected: [1, 2, 3, 2]
got: [1, 2, 2, 2, 3] (using ==)
Diff:
@@ -1,2 +1,2 @@
-[1, 2, 3, 2]
+[1, 2, 2, 2, 3]
# ./spec/models/human_spec.rb:25:in `block (2 levels) in '

3) Brain sets first element the same as other elements
Failure/Error: brain.reload.thoughts.should == [2,2,2,2]
expected: [2, 2, 2, 2]
got: [2, 2, 2] (using ==)
Diff:
@@ -1,2 +1,2 @@
-[2, 2, 2, 2]
+[2, 2, 2]
# ./spec/models/human_spec.rb:30:in `block (2 levels) in '

4) Brain increases size of array
Failure/Error: brain.reload.thoughts.should == [1,2,2,2,2]
expected: [1, 2, 2, 2, 2]
got: [1, 2, 2, 2] (using ==)
Diff:
@@ -1,2 +1,2 @@
-[1, 2, 2, 2, 2]
+[1, 2, 2, 2]
# ./spec/models/human_spec.rb:35:in `block (2 levels) in '

5) Brain decreases size of array
Failure/Error: brain.reload.thoughts.should == [1,2,2]
expected: [1, 2, 2]
got: [1, 2, 2, 2] (using ==)
Diff:
@@ -1,2 +1,2 @@
-[1, 2, 2]
+[1, 2, 2, 2]
# ./spec/models/human_spec.rb:40:in `block (2 levels) in '

Finished in 0.05461 seconds
5 examples, 5 failures
```