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

Cannot Update Hash Field

      Hey guys!

      I'm using Rails 3.2.8 and Mongoid 3.0.8 and I'm having problems updating a Hash field. Basically, my use case is something like this

      class Experience
        include Mongoid::Document
        field :stacks, type: Hash
      
        # ... lots of code ...
      
      end
      

      The 'stacks' field is a hash that contains multiple arrays, like this

      >>> exp.stacks = {
        'stack1' => [1,2,3,4],
        'stack2' => [1,2,3,4],
        'stack3' => [1,2,3,4]  
      }
      
      # ... Updates positions ...
      
      >>> exp.stacks
      {
        'stack1' => [4,3,2,1],
        'stack2' => [1,2,3,4],
        'stack3' => [1,2,3,4]  
      }
      
      >>> exp.save
      >>> exp.stacks
      {
        'stack1' => [1,2,3,4],
        'stack2' => [1,2,3,4],
        'stack3' => [1,2,3,4]  
      }
      
      

      So, if I try to update this document by, say, changing the order between two objects on 'stack1', the hash won't be saved. Things I've tried:

      • Modifying it in-place and using stacks_will_change! to make it dirty: there's nothing on the changes response and changed? is false.
      • Shallow copying using dup: same here.
      • Deep copying the 'stacks' field, changing it and setting it back using Marshal.load(Marshal.dump(stacks)): it actually shows up on changes and changed? is true, but nothing is saved.
      • Hard setting the stacks hash to {} (fit of rage lol): it saves! (of course this it not usable)

      So, I think the problem might be with a) I'm a moron and don't know how to do this or b) It is not detecting deep changes within the Hash. In either case, it's very frustrating because making the attribute dirty manually should make Mongoid save it, whether it agrees or not.

      OBS: Using embedded documents is not an option in this case.
      OBS2: As a curiosity, I couldn't access the saved hash items via symbolic keys, only via string keys. Not a big deal, but I noticed while reading other related issues that this would be the expected behavior.
      OBS3: I read pretty much all related issues from github and also some blog posts (some which suggested monkey patching Object) and still can't make this work

      Thanks!

            Assignee:
            durran Durran Jordan
            Reporter:
            marcelow marcelow
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: