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

Array field changes not persisting

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

      If a field that's an array is modified, and then the field is used in a validation or callback, the field's new value isn't persisted.

      For example, throw this into spec/unit/array_changes_spec.rb in Mongoid's source:

      require "spec_helper"
      
      class Foo 
        include Mongoid::Document
      
        field :bars, :type => Array
      
        before_validation :look_at_bars
      
        def look_at_bars
          bars
          true
        end 
      end
      
      describe 'A document with an array field' do
        it 'updates the array' do
          bars1 = %w(a b)
          bars2 = %w(x y)
          f     = Foo.create :bars => bars1
      
          f.reload
          f.bars.should == bars1
      
          f.bars = bars2
          f.save!
          f.reload
          f.bars.should == bars2
      
          f.delete
        end 
      end
      

      Now run the spec with bundle exec rspec spec/unit/array_changes_spec.rb. Here's the output:

      Run filtered excluding {:config=>#<Proc:./spec/spec_helper.rb:39>}
      
      A document with an array field
        updates the array (FAILED - 1)
      
      Failures:
      
        1) A document with an array field updates the array
           Failure/Error: f.bars.should == bars2
             expected: ["x", "y"]
                  got: ["a", "b"] (using ==)
             Diff:
             @@ -1,2 +1,2 @@
             -["x", "y"]
             +["a", "b"]
           # ./spec/unit/array_changes_spec.rb:28:in `block (2 levels) in <top (required)>'
      
      Finished in 0.00461 seconds
      1 example, 1 failure
      
      Failed examples:
      
      rspec ./spec/unit/array_changes_spec.rb:17 # A document with an array field updates the array
      

      Might anyone be able to suggest how to track down what's causing this problem?

      BTW, I know that's not the correct location for this test file.

            Assignee:
            Unassigned Unassigned
            Reporter:
            nickh Nick Hoffman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: