Skip to content

Instantly share code, notes, and snippets.

@rubish
Last active December 15, 2015 02:48
Show Gist options
  • Save rubish/5189463 to your computer and use it in GitHub Desktop.
Save rubish/5189463 to your computer and use it in GitHub Desktop.
Mongoid duping embedded document doesn't clear metadata
class Address
include Mongoid::Document
field :line1, type: String
field :line2, type: String
field :city, type: String
embedded_in :addressable, polymorphic: true
end
class Person
include Mongoid::Document
field :name, type: String
embeds_one :address, as: :addressable
end
class Company
include Mongoid::Document
field :name, type: String
embeds_many :addresses, as: :addressable
end
p = Person.new(name: "Rubish")
p.build_address(line1: "#113", line2: "Park Plaza", city: "New York")
p.save
c = Company.create(name: "Rubish's Company")
a = p.address.dup
c.addresses << a
# MONGODB (0ms) alma_connect['companies'].update(
# {"_id"=>BSON::ObjectId('514758c175f38e76ce000013')},
# {"$set"=>{"address"=>{"line1"=>"#113", "line2"=>"Park Plaza", "city"=>"New York", "_id"=>BSON::ObjectId('514758c175f38e76ce000014'), "updated_at"=>2013-03-18 18:11:13 UTC, "created_at"=>2013-03-18 18:11:13 UTC}}})
c.reload.addresses #=> [], should have been an array with one address
c['address'] #=> actually the address I was expecting to be in addresses
# reason?
a.metadata #=> metadata from old address embedded in person
Address.new.metadata #=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment