We use #only in our app, and
per https://groups.google.com/forum/?fromgroups=#!searchin/mongoid/only$20identity$20map/mongoid/QsyYKiA0qYI/uZrOokIsX2EJ
we wrap all #only queries in a unit of work like this:
Mongoid.unit_of_work(:disable => :current) do
user = User.only(:email).find(user_id)
end
However, this disables reading from the identity map as well as writing to
the identity map. That is, this makes two database calls:
# Loads User into the identity map
user = User.find(user_id)
# Ignores the identity map, so makes another database call
Mongoid.unit_of_work(:disable => :current) do
user = User.only(:email).find(user_id)
end
Is there a way to disable writing to the identity map so #only queries
don't go into it, but enable the use of the identity map for #only queries?
We have a pretty large application and I'm pretty sure that given this
behavior, we're making unnecessary database calls somewhere. This is fairly
easy to do, too: One class might operate on a User object whereas another
class operates on some object that belongs_to User that only needs access
to certain fields so it uses #only when it gets the User.