Async OneToMany relationship returning just one record


#1

I have two models setup, a User model and a Company model.

/models/company.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  logo: DS.attr('string'),
  employees: DS.attr('number'),
  annualRevenue: DS.attr('number'),
  createdTime: DS.attr('date'),
  updatedTime: DS.attr('date'),
  description: DS.attr('string')
});

/models/user.js

import DS from 'ember-data';

export default DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName');
  }.property('firstName', 'lastName'),
  email: DS.attr('string'),
  enabled: DS.attr('boolean'),
  companies: DS.hasMany('company', {async: true})
});

You’ll notice the async relationship between User > Company. A simple One to Many relationship.

When I request companies from the user model in my example I would expect to find three companies, however I’m only getting one.

userModel.get('companies').then(function(companies){
   console.log( companies.get('length') ); // 1
});

The interesting thing here is, before the promise is resolved there are three company model’s once resolved there is only one. Looking at Ember Inspector I can see that all three companies were saved correctly. However in the user model’s companies key I only find one company. It’s as if Ember Data is preserving the relationship correctly.

Am I doing something wrong here?

Thanks in advance for any help.

Ember: 1.8.1
Ember Data: 1.0.0-beta.12
Handlebars: 1.3.0
jQuery: 1.11.1

#2

Perhaps you need to add DS.belongsTo to company.js


#3

That fixed it, it makes perfect sense as soon as you said that. How else would ember know of the relationship. duh :slight_smile: thanks a bunch.