home
  • Blog
  • Home
  • Projects
    • Ember
    • EmberData
    • Ember CLI
2.18
  • Packages
    • ember-data
  • Classes
    • DS.AbortError
    • DS.Adapter
    • DS.AdapterError
    • DS.AdapterPopulatedRecordArray
    • DS.BelongsToReference
    • DS.BooleanTransform
    • DS.BuildURLMixin
    • DS.ConflictError
    • DS.DateTransform
    • DS.EmbeddedRecordsMixin
    • DS.Errors
    • DS.FilteredRecordArray
    • DS.ForbiddenError
    • DS.HasManyReference
    • DS.InvalidError
    • DS.JSONAPIAdapter
    • DS.JSONAPISerializer
    • DS.JSONSerializer
    • DS.ManyArray
    • DS.Model
    • DS.NotFoundError
    • DS.NumberTransform
    • DS.PromiseArray
    • DS.PromiseManyArray
    • DS.PromiseObject
    • DS.RESTAdapter
    • DS.RESTSerializer
    • DS.RecordArray
    • DS.RecordReference
    • DS.RootState
    • DS.Serializer
    • DS.ServerError
    • DS.Store
    • DS.StringTransform
    • DS.TimeoutError
    • DS.Transform
    • DS.UnauthorizedError
    • Ember.Inflector

Class DS.HasManyReference


Defined in: addon/-private/system/references/has-many.js:11
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.


Methods

ids : Array

Module: ember-data

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

returns
Array

The ids in this has-many relationship

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

Example

app/models/post.js
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']

link : String

Module: ember-data

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

returns
String

The link Ember Data will use to fetch or reload this has-many relationship.

The link Ember Data will use to fetch or reload this has-many relationship.

Example

app/models/post.js
export default DS.Model.extend({
  comments: DS.hasMany({ async: true })
});
let post = store.push({
  data: {
    type: 'post',
    id: 1,
    relationships: {
      comments: {
        links: {
          related: '/posts/1/comments'
        }
      }
    }
  }
});

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

commentsRef.link(); // '/posts/1/comments'

load : Promise

Module: ember-data

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

returns
Promise

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

Loads the relationship if it is not already loaded. If the relationship is already loaded this method does not trigger a new load.

Example

app/models/post.js
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) {
  //...
});

meta : Object

Module: ember-data

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

returns
Object

The meta information for the has-many relationship.

The meta data for the has-many relationship.

Example

app/models/post.js
export default DS.Model.extend({
  comments: DS.hasMany({ async: true })
});
let post = store.push({
  data: {
    type: 'post',
    id: 1,
    relationships: {
      comments: {
        links: {
          related: {
            href: '/posts/1/comments',
            meta: {
              count: 10
            }
          }
        }
      }
    }
  }
});

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

commentsRef.meta(); // { count: 10 }

push (objectOrPromise) : DS.ManyArray

Module: ember-data

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

objectOrPromise
Array|Promise

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

returns
DS.ManyArray

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

app/models/post.js
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']

reload : Promise

Module: ember-data

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

returns
Promise

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

Reloads this has-many relationship.

Example

app/models/post.js
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) {
  //...
});

remoteType : String

Module: ember-data

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

returns
String

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

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

app/models/post.js
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();
}

value : DS.ManyArray

Module: ember-data

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

returns
DS.ManyArray

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

app/models/post.js
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
})
On this page


Methods

  • ids
  • link
  • load
  • meta
  • push
  • reload
  • remoteType
  • value
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2025 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue Created with Sketch.