-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
When deleting a document from a many-to-many referential association, it's expected that the reference is removed in both documents. However, in Mongoid 2.0.1, the reference isn't removed on the other side.
For example, with these 2 models:
class User
include Mongoid::Document
has_and_belongs_to_many :books
end
class Book
include Mongoid::Document
has_and_belongs_to_many :users
end
Deleting a book from a user doesn't cause the user to be deleted from the book:
ruby-1.9.2-p0 > u = User.create
=> #<User _id: 4da4d9392b58af080e000006, _type: nil, _id: BSON::ObjectId('4da4d9392b58af080e000006'), book_ids: []>
ruby-1.9.2-p0 >
ruby-1.9.2-p0 > b = Book.create :users => [u]
=> #<Book _id: 4da4d93f2b58af080e000007, _type: nil, _id: BSON::ObjectId('4da4d93f2b58af080e000007'), user_ids: [BSON::ObjectId('4da4d9392b58af080e000006')]>
ruby-1.9.2-p0 >
ruby-1.9.2-p0 > u.reload
=> #<User _id: 4da4d9392b58af080e000006, _type: nil, _id: BSON::ObjectId('4da4d9392b58af080e000006'), book_ids: [BSON::ObjectId('4da4d93f2b58af080e000007')]>
ruby-1.9.2-p0 >
ruby-1.9.2-p0 > b.reload
=> #<Book _id: 4da4d93f2b58af080e000007, _type: nil, _id: BSON::ObjectId('4da4d93f2b58af080e000007'), user_ids: [BSON::ObjectId('4da4d9392b58af080e000006')]>
ruby-1.9.2-p0 >
ruby-1.9.2-p0 >
ruby-1.9.2-p0 >
ruby-1.9.2-p0 > u.books.delete b
=> #<Book _id: 4da4d93f2b58af080e000007, _type: nil, _id: BSON::ObjectId('4da4d93f2b58af080e000007'), user_ids: []>
ruby-1.9.2-p0 >
ruby-1.9.2-p0 > u.changes
=>
ruby-1.9.2-p0 >
ruby-1.9.2-p0 > u.save
=> true
ruby-1.9.2-p0 >
ruby-1.9.2-p0 >
ruby-1.9.2-p0 >
ruby-1.9.2-p0 > u.reload
=> #<User _id: 4da4d9392b58af080e000006, _type: nil, _id: BSON::ObjectId('4da4d9392b58af080e000006'), book_ids: []>
ruby-1.9.2-p0 >
ruby-1.9.2-p0 > b.reload
=> #<Book _id: 4da4d93f2b58af080e000007, _type: nil, _id: BSON::ObjectId('4da4d93f2b58af080e000007'), user_ids: [BSON::ObjectId('4da4d9392b58af080e000006')]>
ruby-1.9.2-p0 >
ruby-1.9.2-p0 >
ruby-1.9.2-p0 >
ruby-1.9.2-p0 > b.users.empty?
=> false
ruby-1.9.2-p0 >