Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1187

has_and_belongs_to_many + has_many causes extra records to be created on << addition

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.2.0
    • Affects Version/s: None
    • Component/s: None

      I've been struggling with this for most of today Here's finally a simple repro.

      This repro spec in https://github.com/dblock/mongoid/commit/3cb08404f5c3b2b2611d32c6214556e2d637592e, run rspec spec/functional/mongoid/relations/referenced/many_to_many_spec.rb:213.

      Given

      class Exhibitor
      include Mongoid::Document
      belongs_to :exhibition
      has_and_belongs_to_many :artworks
      end

      class Exhibition
      include Mongoid::Document
      has_many :exhibitors
      end

      class Artwork
      include Mongoid::Document
      has_and_belongs_to_many :exhibitors
      end

      let(:artwork) do
      Artwork.create
      end

      let(:exhibition) do
      Exhibition.create
      end

      let(:exhibitor) do
      Exhibitor.create(

      {exhibition: exhibition}

      )
      end

      before do
      artwork.exhibitors << exhibitor
      end

      it "doesn't create additional artwork objects" do
      Artwork.count.should == 1
      end

      Fails:

      1) Mongoid::Relations::Referenced::ManyToMany#push when the relations are not polymorphic when one side has been persisted doesn't create additional person objects
      Failure/Error: Artwork.count.should == 1
      expected: 1
      got: 2 (using ==)

      1. ./spec/functional/mongoid/relations/referenced/many_to_many_spec.rb:195:in `block (6 levels) in <top (required)>'

      2) Mongoid::Relations::Referenced::ManyToMany#concat when the relations are not polymorphic when one side has been persisted doesn't create additional person objects
      Failure/Error: Artwork.count.should == 1
      expected: 1
      got: 3 (using ==)

      1. ./spec/functional/mongoid/relations/referenced/many_to_many_spec.rb:195:in `block (6 levels) in <top (required)>'

            Assignee:
            Unassigned Unassigned
            Reporter:
            dblock Daniel Doubrovkine
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: