{"comment":{"created_at":"2011-10-21T05:59:29Z","body":"Using #find is always going to hit the identity map, and since you aren't clearing it out before going to the database again for the same object that initiated the call you'll always have this situation... It's the same as if you just passed the book instance through, and since the dirty changes don't clear out until all callbacks run you'd be in an infinite loop either way... If you want a different instance of the object you should explicitly clear out the map here:\r\n\r\n```ruby\r\nMongoid::IdentityMap.clear\r\nBook.find(id)update_attributes(:b => \"B\")\r\n```","updated_at":"2011-10-21T05:59:29Z","url":"https://api.github.com/repos/mongoid/mongoid/issues/comments/2478031","id":2478031,"user":{"avatar_url":"https://secure.gravatar.com/avatar/6f09262a13427532461b0b618d445b1e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"6f09262a13427532461b0b618d445b1e","url":"https://api.github.com/users/durran","id":9030,"login":"durran"}},"action":"created","issue":{"number":1367,"created_at":"2011-10-20T22:13:58Z","pull_request":{"diff_url":null,"patch_url":null,"html_url":null},"body":"I have a model that has an after_save callback to update another field if a certain field changes. Today I toyed with turning on the identity map and am not sure if this is a bug or expected behavior, but it crashes. Here's the simplest case which reproduces it. I'm using Mongoid 2.3.0.\r\n\r\n```ruby\r\nclass Book\r\n  include Mongoid::Document\r\n  field :a\r\n  field :b\r\n\r\n  after_save :update_b\r\n  \r\n  def update_b\r\n      if a_changed?\r\n        TestUpdater.perform(id)\r\n      end\r\n  end\r\nend\r\n\r\nclass TestUpdater\r\n  def self.perform(id)\r\n    puts(\"in here\")\r\n    Book.find(id).update_attributes(:b => \"B\")\r\n  end\r\nend\r\n\r\nbook = Book.create\r\nbook.update_attributes(:a => \"A\")\r\n# => prints out \"in here\" until a SystemStackError is raised\r\n```\r\nIf <code>identity_map_enabled: false</code> then this works as intended.\r\n\r\nNote that the workaround is to just change the <code>after_save</code> conditional to check that <code>a_changed? && !b_changed?</code>.","comments":2,"title":"Identity map and updating fields in an after_save callback causing stack overflow.","updated_at":"2011-10-21T05:59:30Z","url":"https://api.github.com/repos/mongoid/mongoid/issues/1367","id":2010099,"assignee":null,"milestone":null,"closed_at":"2011-10-21T05:59:30Z","user":{"avatar_url":"https://secure.gravatar.com/avatar/88326d58697d86446e326c07e5a8afb3?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"88326d58697d86446e326c07e5a8afb3","url":"https://api.github.com/users/jonhyman","id":832655,"login":"jonhyman"},"html_url":"https://github.com/mongoid/mongoid/issues/1367","labels":[],"state":"closed"},"legacy":{"issue_id":2010099,"comment_id":2478031}}