mongoid/mongoid — Issue #1370: In certain situations, concat to create embedded models does not persist but create does with identity map enabled.
Author: jonhyman
Created: 2011-10-21 18:30:38+00:00
State: open

I have an after_create callback for one of my embedded models which updates data in the model (well, it queues up a background job but in dev/test it all happens in serial). When the identity map is enabled, the data does not persist if I concat or << the embedded model into the parent but does persist when I use create.

Example:

```ruby
class Book
include Mongoid::Document
embeds_many :pages
end

class Page
include Mongoid::Document
embedded_in :book
field :text

after_create :update_something

def update_something
update_attributes(:text => "test")
end
end

# Ensure that we have an empty identity map
Mongoid::IdentityMap.clear
Book.destroy_all

b = Book.create
b.pages << Page.new
b.pages
# => [#]

# Clear out the identity map to ensure the next find call hits the database
Mongoid::IdentityMap.clear
Book.find(b.id).pages
# => []
# would have expected this to contain a Page
```
If I change b.pages << Page.new to b.pages.create, everything works fine. Alternatively, I can call Mongoid::IdentityMap.clear in the callback and it'll work regardless of how the create occurs.