@hasMany

Declares a one-to-many or many-to-many relationship on a model.

Since Ember 4/5 (Octane) Spec ↗

Syntax

@hasMany('type', { async, inverse })

Returns

PropertyDecorator — A collection of related records.

Examples

import Model, { hasMany, attr } from '@ember-data/model';

export default class PostModel extends Model {
  @attr('string') title;
  @hasMany('comment', { async: false, inverse: 'post' }) comments;
  @hasMany('tag', { async: true, inverse: 'posts' }) tags;
}
const comments = await post.comments;
comments.push(this.store.createRecord('comment', { body: 'Nice!' }));
await post.save();

Notes

async: true returns a promise-like ManyArray loaded on access; async: false requires the records to be present already. Push/remove on the ManyArray and call save() to persist changes.