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

Attributes mutated in place are not persisted

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Attributes
    • Labels:
    • Story Points:
      4

      Description

      If a field is a container object (Set or Array, for example), changes to this object are not recognized by Mongoid and are not persisted.

      The value of the attribute must be reset for Mongoid to persist the new data.

      This issue was originally filed for Set fields, https://jira.mongodb.org/browse/MONGOID-3743 is another instance of this issue for Array fields.


      Not sure what's going on here but I cannot add to sets embedded in a mongoid document.

      class Foo
        include Mongoid::Document
       
        field :test, type: Set, default: -> {Set.new}
      end
      

      same result with default: Set.new

      >> f = Foo.new(test: Set.new)
      => #<Foo _id: 5162efe3463a990219000001, test: []>
      >> f.test.add 1
      => #<Set: {1}>
      >> f.test
      => #<Set: {}>
      >> f.test.object_id
      => 70154345163900
      >> f.test.object_id
      => 70154345825180
      


      Here is an example with a string field:

      > foo = Foo.first
      > foo.bar
      "My random value"
      > foo.bar.gsub!(/random/, '')
      "My  value"> foo.changed?
      false
      > foo.save # does not changes value of bar to "My  value"
      > foo.bar
      "My  value"
      > foo = Foo.first
      > foo.bar
      "My  value" 

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                1 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated: