#

API Docs for: v3.8.1
Show: Inherited Protected Private Deprecated

# DS.HasManyReference Class

Defined in: `addon/-private/system/references/has-many.js:8`
Module: ember-data

A HasManyReference is a low-level API that allows users and addon author to perform meta-operations on a has-many relationship.

- Index
- Methods

## Item Index

### Methods

- ids
- load
- push
- reload
- remoteType
- value

## Methods

### `ids`

() Array

Defined in `addon/-private/system/references/has-many.js:79`

`ids()` returns an array of the record IDs in this relationship.

Example

    export default DS.Model.extend({
      comments: DS.hasMany({ async: true })
    });

    let post = store.push({
      data: {
        type: 'post',
        id: 1,
        relationships: {
          comments: {
            data: [{ type: 'comment', id: 1 }]
          }
        }
      }
    });

    let commentsRef = post.hasMany('comments');

    commentsRef.ids(); // ['1']

#### Returns:

Array:

The ids in this has-many relationship

### `load`

(

- `options`

)
Promise

Defined in `addon/-private/system/references/has-many.js:257`

Loads the relationship if it is not already loaded. If the relationship is already loaded this method does not trigger a new load. This causes a request to the specified relationship link or reloads all items currently in the relationship.

Example

    export default DS.Model.extend({
      comments: DS.hasMany({ async: true })
    });

    let post = store.push({
      data: {
        type: 'post',
        id: 1,
        relationships: {
          comments: {
            data: [{ type: 'comment', id: 1 }]
          }
        }
      }
    });

    let commentsRef = post.hasMany('comments');

    commentsRef.load().then(function(comments) {
      //...
    });

You may also pass in an options object whose properties will be fed forward. This enables you to pass `adapterOptions` into the request given to the adapter via the reference.

Example

    commentsRef.load({ adapterOptions: { isPrivate: true } })
      .then(function(comments) {
        //...
      });

    export default ApplicationAdapter.extend({
      findMany(store, type, id, snapshots) {
        // In the adapter you will have access to adapterOptions.
        let adapterOptions = snapshots[0].adapterOptions;
      }
    });

#### Parameters:

- `options` Object

  the options to pass in.

#### Returns:

Promise:

a promise that resolves with the ManyArray in this has-many relationship.

### `push`

(

- `objectOrPromise`

)
DS.ManyArray

Defined in `addon/-private/system/references/has-many.js:122`

`push` can be used to update the data in the relationship and Ember Data will treat the new data as the canonical value of this relationship on the backend.

Example

    export default DS.Model.extend({
      comments: DS.hasMany({ async: true })
    });

    let post = store.push({
      data: {
        type: 'post',
        id: 1,
        relationships: {
          comments: {
            data: [{ type: 'comment', id: 1 }]
          }
        }
      }
    });

    let commentsRef = post.hasMany('comments');

    commentsRef.ids(); // ['1']

    commentsRef.push([
    [{ type: 'comment', id: 2 }],
    [{ type: 'comment', id: 3 }],
    ])

    commentsRef.ids(); // ['2', '3']

#### Parameters:

- `objectOrPromise` Array | Promise

  a promise that resolves to a JSONAPI document object describing the new value of this relationship.

#### Returns:

DS.ManyArray:

### `reload`

(

- `options`

)
Promise

Defined in `addon/-private/system/references/has-many.js:322`

Reloads this has-many relationship. This causes a request to the specified relationship link or reloads all items currently in the relationship.

Example

    export default DS.Model.extend({
      comments: DS.hasMany({ async: true })
    });

    let post = store.push({
      data: {
        type: 'post',
        id: 1,
        relationships: {
          comments: {
            data: [{ type: 'comment', id: 1 }]
          }
        }
      }
    });

    let commentsRef = post.hasMany('comments');

    commentsRef.reload().then(function(comments) {
      //...
    });

You may also pass in an options object whose properties will be fed forward. This enables you to pass `adapterOptions` into the request given to the adapter via the reference. A full example can be found in the `load` method.

Example

    commentsRef.reload({ adapterOptions: { isPrivate: true } })

#### Parameters:

- `options` Object

  the options to pass in.

#### Returns:

Promise:

a promise that resolves with the ManyArray in this has-many relationship.

### `remoteType`

() String

Defined in `addon/-private/system/references/has-many.js:31`

This returns a string that represents how the reference will be looked up when it is loaded. If the relationship has a link it will use the "link" otherwise it defaults to "id".

Example

    export default DS.Model.extend({
      comments: DS.hasMany({ async: true })
    });

    let post = store.push({
      data: {
        type: 'post',
        id: 1,
        relationships: {
          comments: {
            data: [{ type: 'comment', id: 1 }]
          }
        }
      }
    });

    let commentsRef = post.hasMany('comments');

    // get the identifier of the reference
    if (commentsRef.remoteType() === "ids") {
      let ids = commentsRef.ids();
    } else if (commentsRef.remoteType() === "link") {
      let link = commentsRef.link();
    }

#### Returns:

String:

The name of the remote type. This should either be `link` or `ids`

### `value`

() DS.ManyArray

Defined in `addon/-private/system/references/has-many.js:211`

`value()` synchronously returns the current value of the has-many relationship. Unlike `record.get('relationshipName')`, calling `value()` on a reference does not trigger a fetch if the async relationship is not yet loaded. If the relationship is not loaded it will always return `null`.

Example

    export default DS.Model.extend({
      comments: DS.hasMany({ async: true })
    });

    let post = store.push({
      data: {
        type: 'post',
        id: 1,
        relationships: {
          comments: {
            data: [{ type: 'comment', id: 1 }]
          }
        }
      }
    });

    let commentsRef = post.hasMany('comments');

    post.get('comments').then(function(comments) {
      commentsRef.value() === comments
    })

#### Returns:

DS.ManyArray: