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

Mongoid 4.0.0 change in habtm behavior when assigning via '='

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      I am updating a project to Mongoid 4 and experiencing a change in has_and_belongs_to_many behavior that I am not seeing in the docs.

      When updating a relation with object.relation = [other_objects], I'm seeing the inverse relation get dropped.

      This seems similar to MONGOID-3246

      class Project
        include Mongoid::Document
        has_and_belongs_to_many :team_members
      end
      
      class TeamMember
        include Mongoid::Document
        field :name, type: String
        has_and_belongs_to_many :projects
      end
      
      project = Project.create()
      amy = TeamMember.create(name: 'amy')
      steve = TeamMember.create(name: 'steve')
      
      # Add Amy and Steve to the project
      project.team_members = [amy, steve]
      [project, amy, steve].each(&:reload)
      
      # Data looks good
      puts project.team_members.pluck(:name).inspect # => ['amy', 'steve']
      puts amy.projects.count # => 1
      puts steve.projects.count # => 1
      
      # Remove Steve from the project, should just be Amy now
      project.team_members = [amy]
      [project, amy, steve].each(&:reload)
      
      # Amy has no projects now!
      puts project.team_members.pluck(:name).inspect # => ['amy']
      puts amy.projects.count # => 0 Amy should still be on the project! Returns 1 in Mongoid 3
      puts steve.projects.count # => 0
      

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            jonzeper jonzeper
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: