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

"Has Many" with Hash Attribute Returns Nil

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

      Hey guys,

      Been banging my head on the keyboard for over an hour with this one - I feel like it's my stupidity, but just not seeing it..
      I've simplified this code a bit, but the process is the same.

      class Foo
        include Mongoid::Document
        has_many :bars
      
        def add_bar!(name, metadata = { })
          bars.create! :name => name, :metadata => metadata
        end
      end
      
      class Bar
        include Mongoid::Document    
        belongs_to :foo
        field :name, :type => String
        field :metadata,      :type => Hash
      end
      
      f = Foo.create!
      b = f.add_bar!("oh hai", { :something => "blah" })
      # at this point, b.metadata looks good, BUT
      Bar.find(b.id).metadata.nil? # returns true
      

      I don't understand how the create! can report that everything has saved OK, yet the Hash data clearly has not been saved?

      Many TIA.
      Rob

            Assignee:
            Unassigned Unassigned
            Reporter:
            robcthegeek Rob Cooper
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: