2

Let's say I have some model like:

App.Employee = DS.Model.extend({
  name: DS.attr('string'),

  department: DS.belongsTo('App.Department')    
});

In my controller I can say

var name = thisEmployee.get('name');

But I can't say

var department = thisEmployee.get('department');

So my question is how to get a reference to the object on the other side of the relationship.

onezeno
  • 764
  • 1
  • 10
  • 21
  • 1
    why you can't say `thisEmployee.get('department');` in your controller, what happens? do you get an error? or it returns null? – intuitivepixel May 14 '13 at 15:51

1 Answers1

3

I assume that the problem lies in the fact that you maybe don't have specified how your relationships should load... try setting up your adapter map like this:

App.Adapter.map('App.Employee', {
  department: {embedded: 'always'}
});

This should side load your department relationships automatically on requesting the parent model Employee with e.g. App.Employee.find();.

Hope it helps

intuitivepixel
  • 23,302
  • 3
  • 57
  • 51
  • That was it :) One thing to note: it didn't work until I moved the code to be before the App.Store block, where the adapter is created. – onezeno May 14 '13 at 16:54
  • 2
    Where is this documented? – kmiyashiro Jul 19 '13 at 18:24
  • 1
    @kmiyashiro, nowhere. You find this out rather in two ways 1. reading the source code, or 2.see this answer from Yehuda Katz (ember core team) http://stackoverflow.com/questions/14320925/how-to-make-embedded-hasmany-relationships-work-with-ember-data – intuitivepixel Jul 19 '13 at 19:08