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

Calling include? messes up relationship

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

      I discovered this rather odd bug when I added authorization to my application. I noticed that when I added an extra iteration on a relationship in the authorization code, strange things happened in my view - references in the relationships multiplied!

      I reproduced the issue as follows:

      class Foo
        include Mongoid::Document
        has_and_belongs_to_many :bars, :class_name => 'Bar', :inverse_of => :foos
      end
      
      class Bar
        include Mongoid::Document
        has_and_belongs_to_many :foos, :class_name => 'Foo', :inverse_of => :bars
      end
      
      require 'mongoid'
      require './foo'
      require './bar'
      
      Mongoid.database = Mongo::Connection.new('localhost','27017').db('bugs')
      Mongoid.master.collections.reject { |c| c.name =~ /^system/}.each(&:drop) # Clean sheet
      
      foo = Foo.create
      foo.bars << Bar.create
      puts foo.bars.count # 1, as expected
      
      foo = Foo.first
      puts foo.bars.to_a.count # 1, as expected
      puts foo.bars.to_a.count # 1, as expected
      
      foo = Foo.first
      bar = Bar.first
      puts foo.bars.include?(bar) # true, as expected
      puts foo.bars.to_a.count # 1, as expected
      puts foo.bars.to_a.count # 2, not expected!
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            averas averas
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: