mongoid/mongoid — Issue #1463: Saving unrelated model in observer after_save creates extra record on wrong model
Author: wulffeld
Created: 2011-11-30 12:34:52+00:00
State: open

Given these models and observer:

```
class Project
include Mongoid::Document

field :title

has_many :tasks, :dependent => :destroy

accepts_nested_attributes_for :tasks, :allow_destroy => true
end

class Task
include Mongoid::Document

field :title

belongs_to :project
end

class Log
include Mongoid::Document

field :message
end

class TaskObserver < Mongoid::Observer
def after_save(task)
Log.create!(:message => "Task created")
end
end
```

Console (with 100% empty db):

```
>> p = Project.create!
>> p.tasks_attributes = [{:title => "My Task"}]
[
[0] {
:title => "My Task"
}
]

>> Project.first.tasks.to_a
[
[0] #,
[1] #
]

>> Log.all.to_a
[]
```

As can be seen an extra Task record with the :message field and no Log records has been created.