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

Overwriting a HABTM relation twice leads to empty inverse foreign key

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

      When overwriting a HABTM relation twice with the same value, the inverse foreign key ends up being empty:

      class A
        include Mongoid::Document
        has_and_belongs_to_many :bs, inverse_of: :as
      end
      
      class B
        include Mongoid::Document
        has_and_belongs_to_many :as, inverse_of: :bs
      end
      
      a = A.create
      b = B.create
      
      a.bs = [b]
      puts a.reload.b_ids.inspect # => [id]
      puts b.reload.a_ids.inspect # => [id]
      
      a.bs = [b]
      puts a.reload.b_ids.inspect # => [id]
      puts b.reload.a_ids.inspect # => []
      

      Please see attached pull request for a corresponding failing spec. The commit also applies cleanly to master and the spec fails there as well.

            Assignee:
            durran Durran Jordan
            Reporter:
            niels Niels Ganser
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: