protected function getAbilitiesPivotQuery(Model $model, $ids)
    {
        $relation = $model->abilities();

        list($foreignKeyName, $relatedKeyName) = $this->getRelationKeyNames($relation);

        $query = $relation
            ->newPivotStatement()
            ->where($foreignKeyName, $model->getKey())
            ->where('entity_type', $model->getMorphClass())
            ->whereIn($relatedKeyName, $ids);

        return Models::scope()->applyToRelationQuery(
            $query, $relation->getTable()
        );
    }