How to create and save a model with a belongsTo relationship in Ember-Data


#1

I’m getting the next error when I’m trying to save a new record with an associated relationship. The problem I think is related to the creation of the record because ember doesn’t create any record.

Uncaught TypeError: Cannot call method 'save' of undefined

On the one hand, my models are the following (with a one-to-many relationship).

App.Kid = DS.Model.extend({
    attribute1: DS.attr("string"),
    parent: DS.belongsTo(parent)
});

App.Parent = DS.Model.extend({
    attribute1: DS.attr("string"),
    kids: DS.hasMany(kid)
});

On the other hand, my route is the following.

App.KidRoute = Ember.Route.extend({
    model: function (id) {
        var store = this.get('store');
        store.find('usuario', id).then(function(parentmodel){
            return this.store.createRecord('kid', {parent: parentmodel});
        });
    },
    actions:{
        save: function(){
            this.get('currentModel').save();
        }
    }
});

Any help would be appreciate. Thanks!

Editing…

It is necessary to set the mapping on the adapter through embedded: 'always'? Any thoughts?


#2

hey there, just a suggestion, people here usually does not reply to this type of questions, it would be better to ask in http://stackoverflow.com/questions/tagged/ember.js . This forum is mostly used for ideas, suggestions, and to discuss new features.


#3

I’d already posted there but I thought this forum would be more helpful for ember questions. Thanks for the clarification!


#4

@alopez, can you please share the Stack Overflow link for this question? I was wondering about this same thing. Did you figure out how to do it? How to save the id for a related record.