Skip to content

Commit

Permalink
Model#becomes properly keeps the same id. [ fix #2504 ]
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Oct 27, 2012
1 parent c22d85e commit 576617a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ For instructions on upgrading to newer versions, visit

### Resolved Issues

* \#2504 `Model#becomes` properly keeps the same id.

* \#2494 All explicit atomic operations now properly respect aliased fields.

* \#2491 Removed unnecessary merge call in cascadable children.
Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def becomes(klass)
raise ArgumentError, "A class which includes Mongoid::Document is expected"
end
became = klass.new(as_document.__deep_copy__)
became.id = id
became.instance_variable_set(:@changed_attributes, changed_attributes)
became.instance_variable_set(:@errors, errors)
became.instance_variable_set(:@new_record, new_record?)
Expand Down
4 changes: 4 additions & 0 deletions spec/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,10 @@ class Manager < Person
became.title.should eq('Sir')
end

it "keeps the same object id" do
became.id.should eq(obj.id)
end

context "when the document has embedded documents" do

let!(:address) do
Expand Down

0 comments on commit 576617a

Please sign in to comment.