-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: 5.1.2
-
Component/s: None
-
None
Hi,
I have an issue with self-referencing has_and_belongs_to_many relations.
With the following model and test case, the last two tests fail, while I expected them to succeed:
```ruby
class Item
include Mongoid::Document
has_and_belongs_to_many :parents, inverse_of: :children, class_name: 'Item'
has_and_belongs_to_many :children, inverse_of: :parents, class_name: 'Item'
end
RSpec.describe Item, type: :model do
describe '#create' do
let!(:parent)
let!(:child)
{ Item.create(parents: [parent]) } before do
parent.reload
child.reload
end
it
{ expect(child.parents).to include(parent) }it { expect(child.parent_ids).to include(parent.id) }
it { expect(parent.children).to include(child) }
it { expect(parent.child_ids).to include(child.id) }
describe '#update' do
before do
child.update(parents: [parent])
end
it { expect(child.parents).to include(parent) }
it
{ expect(child.parent_ids).to include(parent.id) }it { expect(parent.children).to include(child) }
it { expect(parent.child_ids).to include(child.id) }
describe '#reload' do
before do
parent.reload
child.reload
end
it { expect(child.parents).to include(parent) }
it { expect(child.parent_ids).to include(parent.id) }
it
{ expect(parent.children).to include(child) }it
{ expect(parent.child_ids).to include(child.id) } end
end
end
end
```
Thanks in advance!
- duplicates
-
MONGOID-3090 referenced n-n relation incomplete tracking
- Closed
- is duplicated by
-
MONGOID-3604 Add test for persisting inverse keys for HABTM substitution
- Closed
-
MONGOID-3849 Mongoid 4.0.0 change in habtm behavior when assigning via '='
- Closed