-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
Labels:None
I have three models to set this up:
- Parent: embeds_many :children
- Child: embedded_in :parent, has_and_belongs_to_many :toys, inverse_of: nil
- Toy
Creating the embedded child object along with the parent seems to be the cause. Also, this does not seem to be an issue when using the << operator to set toys instead of toy_ids.
I have verified this fails in versions: v3.0.0 - v3.0.19 and master(3.1.0). It is currently working in v2.5.2 and 2.6.0.
require 'spec_helper' class Parent include Mongoid::Document field :name, type: String embeds_many :children, class_name: "Child" end class Child include Mongoid::Document embedded_in :parent field :name, type: String has_and_belongs_to_many :toys, inverse_of: nil end class Toy include Mongoid::Document field :type, type: String end describe Parent do before :all do Parent.delete_all Toy.delete_all end # This is broken it 'should be able to set child.toy_ids without a reload' do parent = Parent.new(name: "Dexter") parent.children << Child.new(name: "Harrison") parent.save! # parent.reload # reload makes it work child = parent.children.first toy = Toy.create(type: "TRex") child.toy_ids << toy._id child.toys.first.should == toy end # This works it 'should be able to set child.toys without a reload' do parent = Parent.new(name: "Dexter") parent.children << Child.new(name: "Harrison") parent.save! child = parent.children.first toy = Toy.create(type: "TRex") child.toys << toy child.toys.first.should == toy end end