Search code examples
ember.jsmodelrelationships

Getting my Post models' Comments to load and render


I'm endeavouring to build a blog of sorts in Ember. One in which I can log in (not there yet), make posts, and then leave comments and add posts to favourites. Right now, I can't seem to get my posts' comments to load at all — I assumed that once I had the relationships defined, they'd load themselves and I could access them at any point.

Here's my JSBin of the work in progress:

http://jsbin.com/nog/3/edit


Solution

  • You need to change post: ['2'] to post: '2' (since it's a belongsTo relationship it expects a value not an array):

    App.Comment = DS.Model.extend({
        post: DS.belongsTo('post', { async: true }),
        date_posted: DS.attr('date'),
        author: DS.attr('string'),
        message: DS.attr('string')
    });
    
    App.Comment.FIXTURES = [{
        id: '1',
        post: '2',
        date_posted: new Date(),
        author: 'Aaron',
        message: 'Psyched for the third.'
    }];
    

    See http://jsbin.com/daqonone/1/