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

Model#becomes changes an object id

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

      It seems that Model#becomes method changes an object id. I have this problem after I upgraded mongoin in my app to v3.0.9.

      My quick fix for this issue is:

      Unable to find source-code formatter for language: diff. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      diff --git a/lib/mongoid/document.rb b/lib/mongoid/document.rb
      index 42480cd..12ebff0 100644
      --- a/lib/mongoid/document.rb
      +++ b/lib/mongoid/document.rb
      @@ -218,6 +218,7 @@ module Mongoid
               raise ArgumentError, "A class which includes Mongoid::Document is expected"
             end
             became = klass.new(as_document.__deep_copy__)
      +      became._id = self._id
             became.instance_variable_set(:@changed_attributes, changed_attributes)
             became.instance_variable_set(:@errors, errors)
             became.instance_variable_set(:@new_record, new_record?)
      diff --git a/spec/mongoid/document_spec.rb b/spec/mongoid/document_spec.rb
      index 3f0f259..2fc918e 100644
      --- a/spec/mongoid/document_spec.rb
      +++ b/spec/mongoid/document_spec.rb
      @@ -920,6 +920,10 @@ describe Mongoid::Document do
                 it "copies the state" do
                   became.should be_persisted
                 end
      +
      +          it "does not change an id" do
      +            became.id.should == obj.id
      +          end
               end
       
               context "when the document is destroyed" do
      ~
      ~
      
      

      In my opinion sth could be broken in the document's constructor, I think _id field is ignored and somehow a new id is generated.

            Assignee:
            Unassigned Unassigned
            Reporter:
            lucassus lucassus
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: