Function

modelName
String
(optional) type of the relationship
options
Object
(optional) a hash of options
returns
Ember.computed
relationship

belongsTo is used to define One-To-One and One-To-Many relationships on a Model.

belongsTo takes an optional hash as a second parameter, currently supported options are:

  • async: A boolean value used to explicitly declare this to be an async relationship. The default is true.
  • inverse: A string used to identify the inverse property on a related model in a One-To-Many relationship. See Explicit Inverses
  • polymorphic A boolean value to mark the relationship as polymorphic

One-To-One

To declare a one-to-one relationship between two models, use belongsTo:

app/models/user.js
1
2
3
4
5
6
7
8
import Model, { belongsTo } from '@ember-data/model';

export default class UserModel extends Model {
  @belongsto('post', {
    async: false
  })
  post;
}

In contrast to async relationship, accessing a sync relationship will always return the record (Model instance) for the existing local resource, or null. But it will error on access when a related resource is known to exist and it has not been loaded.

1
let post = comment.post;

@hasmany('comment') comments; }

1
2
3
4
5
```app/models/comment.js
import Model, { belongsTo } from '@ember-data/model';

export default class CommentModel extends Model {