Skip to content

Instantly share code, notes, and snippets.

@shawn42
Created April 12, 2011 14:23
Show Gist options
  • Save shawn42/915582 to your computer and use it in GitHub Desktop.
Save shawn42/915582 to your computer and use it in GitHub Desktop.
# HELLO?
class Foo
include Mongoid::Document
field :stats, :type => Hash
end
f = Foo.create(:stats => {"bar" => {}})
f.stats["bar"]["baz"] = "qux"
# => <Foo _id: 4da45df77654aa7729000004, stats: {"bar"=>{"baz"=>"qux"}}>
f.save!
# => true
# looks good
f.stats["bar"]
# => {"baz"=>"qux"}
# oh noes, what happened?
f.reload.stats["bar"]
# => {}
class Object
def deep_copy
Marshal.load(Marshal.dump(self))
end
end
f = Foo.create(:stats => {"bar" => {}})
stats_copy = f.stats.deep_copy
stats_copy["bar"]["baz"] = "qux"
f.stats = stats_copy
f
# => #<Foo _id: 4da45f987654aa7729000005, stats: {"bar"=>{"baz"=>"qux"}}>
f.save!
# => true
# looks good
f.stats["bar"]
# => {"baz"=>"qux"}
# victory...
f.reload.stats["bar"]
# => {"baz"=>"qux"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment