mongoid/mongoid — Issue #1447: Bug with identity_map_enabled and queries using without() and only().
Author: cezarsa
Created: 2011-11-23 16:34:34+00:00
State: open

It seems like mongoid is storing incomplete objects inside the identity map, check examples below:

Without identity_map_enabled

$ rails console
Loading development environment (Rails 3.1.1)
ruby-1.9.2-p290 :001 > s = Show.without(:episodes).first
=> #
ruby-1.9.2-p290 :002 > s.episodes.size
=> 0
ruby-1.9.2-p290 :003 > s = Show.find('4ec30e7722bbec0007000000')
=> #
ruby-1.9.2-p290 :004 > s.episodes.size
=> 33

With identity_map_enabled: true

$ rails console
Loading development environment (Rails 3.1.1)
ruby-1.9.2-p290 :003 > s = Show.without(:episodes).first
=> #
ruby-1.9.2-p290 :004 > s.episodes.size
=> 0
ruby-1.9.2-p290 :005 > s = Show.find('4ec30e7722bbec0007000000')
=> #
ruby-1.9.2-p290 :006 > s.episodes.size
=> 0

I'm not sure about what would be the correct behavior. IMO mongoid should either make a new query fetching the empty fields or never include incomplete objects inside the identity map.