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

HABM relations are not saved correctly on both sides if providing just model_ids

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

      So I have the following models set up

      class User
        include Mongoid::Document
        has_and_belongs_to_many :teams, autosave: true
      end
      
      class Team
        include Mongoid::Document
        has_and_belongs_to_many :users, autosave: true
      end
      

      When I create a Team with team = Team.create users: [user] where user is a user object, everything works fine and my newly created team appears in user.teams. However, I’m getting data via POST so I only receive user ids, which leads me more or less to the following code: team = Team.create user_ids: [user.id.to_s] (production code looks of course different, user_ids are provided via params)

      This also works as expected, except that the team is not stored in the user model. user.teams is still empty while team.users is saved correctly. Is this the desired behaviour?

      I am using Mongoid 4 with Rails 4.

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

              Created:
              Updated:
              Resolved: