mongoid/mongoid — pull request #2310 by Nielsomat (2012-08-22 13:09:29+00:00)
Title: Overwriting a HABTM relation twice leads to empty inverse foreign key

When overwriting a HABTM relation twice with the same value, the inverse foreign key ends up being empty:

```ruby
class A
include Mongoid::Document
has_and_belongs_to_many :bs, inverse_of: :as
end

class B
include Mongoid::Document
has_and_belongs_to_many :as, inverse_of: :bs
end

a = A.create
b = B.create

a.bs = [b]
puts a.reload.b_ids.inspect # => [id]
puts b.reload.a_ids.inspect # => [id]

a.bs = [b]
puts a.reload.b_ids.inspect # => [id]
puts b.reload.a_ids.inspect # => []
```

Please see attached pull request for a corresponding failing spec. The commit also applies cleanly to master and the spec fails there as well.