How to name a belongstomany association in controller with cakephp 4.4

I need to filter records by a field in a belongstomany association (tables are Users and Groups, intermediate table is UsersGroups) but when I put UsersGroups.group_id in the where clause it gives me exception The UsersGroups association is not defined on Users.
How can I solve this issue?
This is the code for index action:

    public function index()
    {
        $this->paginate = [
            'contain' => ['Profiles', 'Referrer', 'UsersGroups.Groups', 'UsersGroups.Nets'],
        ];
        if ($this->controllerUser['isAdmin'] || $this->controllerUser['isGeneralCoordinator']){
            $users = $this->paginate($this->Users->find('all')->where(['Users.profile_id IN' => [1,2,3,4,5,6,7]]));
        } else if ($this->controllerUser['isGroupCoordinator']){
            $users = $this->paginate($this->Users->find('all')->where(['UsersGroups.group_id in' => $this->controllerUser['group_id'], 
                'Users.profile_id IN' => [3,4,5,6]]));
        } else if ($this->controllerUser['isNetCoordinator']){
            $users = $this->paginate($this->Users->find('all')->where(['UsersGroups.group_id' => $this->controllerUser['group_id'], 
                'UsersGroups.net_id' => $this->controllerUser['net_id'], 
                'Users.profile_id IN' => [4,5,6,7]]));
        } else if ($this->controllerUser['isNodeCoordinator']){
            $users = $this->paginate($this->Users->find('all')->where(['UsersGroups.group_id' => $this->controllerUser['group_id'], 
                'UsersGroups.net_id' => $this->controllerUser['net_id'], 
                'UsersGroups.main_gate' => $this->controllerUser['main_gate'], 
                'Users.profile_id IN' => [4,5,6,7]]));
        } else if ($this->controllerUser['isTacticOperator']){
            $users = $this->paginate($this->Users->find('all')->where(['UsersGroups.group_id' => $this->controllerUser['group_id'], 
                'UsersGroups.net_id' => $this->controllerUser['net_id'], 
                'UsersGroups.main_gate' => $this->controllerUser['main_gate'], 
                'Users.profile_id IN' => [4,5,6,7]]));

        } else if ($this->controllerUser['isSanitaryAgent'] ){
            $users = $this->paginate($this->Users->find('all')->where(['UsersGroups.group_id' => $this->controllerUser['group_id'], 
                'UsersGroups.net_id' => $this->controllerUser['net_id'], 
                'UsersGroups.main_gate' => $this->controllerUser['main_gate'], 
                'Users.profile_id IN' => [4,5,6,7]]));
        }

        $this->set(compact('users'));
    }

belongsToMany relations are done in a separate query. What you’re looking for is matching?

Yes, I looked in google and found that, but I also need to filter by another field of the intermediate table UsersGroups, this has this fields: id, user_id, group_id, net_id, main_gate and the typical created and modified, but I need to filter by group_id, net_id and main_gate, how can I do that?

Does the belongsToMany association is being made in your UsersTable.php?

The problem is that Users is associated to Groups and not directly associated to the joinTable.
The easy way is to define the association that is not defined. In some cases it can be handy…