MongoMapper::Plugins::Clone.initialize_copy assumes @_id and @_id_before_type_cast are set #498

Closed
luis-ca opened this Issue Feb 4, 2013 · 3 comments

Projects

None yet

2 participants

@luis-ca
luis-ca commented Feb 4, 2013

When cloning a document, @_id_before_type_cast is not set. Chaining clone calls will then break due to Clone trying to unset @_id_before_type_cast.

o = SomeDocument.new
o2 = o.clone
o2.clone # raises NameError: instance variable @_id_before_type_cast not defined

In fact, chaining clones produces another error

o = SomeDocument.new
o.clone.clone # raises NameError: instance variable @_id

I am not sure whether this is a problem with Clone or more interestingly, a problem with how documents are initialised.

@jnunemaker
Collaborator

I guess the only way it could happen is if you pass a nil id or some id that is not defaulted.

@luis-ca
luis-ca commented Feb 4, 2013

It happens on a regular old document.

class User
    include MongoMapper::Document
end

User.new.clone.clone # raises NameError: instance variable @_id not defined
@cheald cheald added a commit that closed this issue Jul 6, 2013
@cheald cheald Don't remove @_id if it's not already set; this is possible since @_i…
…d is lazy-assigned on access. Closes #498.
e4c7943
@cheald cheald closed this in e4c7943 Jul 6, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment