mongoid/mongoid — Issue #1731: "Has Many" with Hash Attribute Returns Nil
Author: robcthegeek
Created: 2012-02-19 14:44:35+00:00
State: open

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.

```ruby
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