How to get a model's name attribute?


#1

This is really confusing me at the moment and it’s probably dead simple to fix.

I have two model, related between themselves:

App.User = DS.Model.extend({
  name: DS.attr('string'),
  zip_code: DS.attr('string'),
  address_line: DS.attr('string'),
  country: DS.attr('string'),
  active: DS.attr('boolean'),
  state: DS.attr('string'),
  comments: DS.attr('string'),
  user_type: DS.belongsTo('user_type', { async: true })
});

App.UserType = DS.Model.extend({
  name: DS.attr('string'),
  users: DS.hasMany('users', { async: true })
});

I’m trying to the a User's UserType.name:

Simple enough right? Wrong!

// Doesn't work.
user.get('user_type.name');

// Also doesn't work.
user.get('user_type').get('name');

In my console, I can clearly see the object is there and related.

enter image description here


#2

You’ve declared the relationship as async which means it is wrapped in a promise. You should be able to do something like:

user.get(‘user_type’).then(function (user_type) { return user_type.get(‘name’)}). then(function (name) { … whatever});

(Excuse formatting, on the bus)


#3

and you should move to camelCase. user_type -> userType