Ember data error: Assertion Failed: You can only add a 'user' record to this relationship


#1

I have a ember model like this:

var attr = DS.attr,
    hasMany = DS.hasMany,
    belongsTo = DS.belongsTo;
App.Message = DS.Model.extend({
    message: attr(),
    user: belongsTo('user'),
    inquiry: belongsTo('inquiry')
});

I want to createRecord like this:

var createMessage = this.store.createRecord('message', {
    'message': "test message",
    'user': 1,
    'inquiry': 1
});
createMessage.save();

I know this question has been asked several times but I can’t find any clear solution. can someone please describe me why this error happening and what will be the solution.I am using Ember: 1.6.1 and Ember-data: 1.0.0-beta.5


#2

The error message says that you are trying to add an object that is not derived from the user model (App.User = DS.User.extend …).

Can you show the Usermodel definition and the object you are trying to add? And another thing:

message: attr(),

should be

message: attr('string'),

Update belongsTo relation from dropdown (ember select view)?
#3

how do I pass user model. Do I need to do something like this:

var message = this.store.createRecord('message');
var self = this;
self.store.find('user', 39).then(function(user){
    message.set('user', user);
    self.store.find('inquiry', 2).then(function(inquiry){
        message.set('inquiry', inquiry);
        message.set('message', "hello message");
        message.save();
    });
});

#4

it looks about right to me


#5

Yes it worked for me. But my question is I am making two extra request to my backend to get user and inquiry model. For example if I have three or four belongs field; then I will have to make three or four extra request to my backend to get those model. Is it right way to do it ? I have another question for you: whats the best way to save currentUser model that way other controller can use it. Thanks for your time and response.


#6

you can inject the current user to all routes and controllers. this might help: http://wrktg.com/articles/understanding-application-register-and-inject-methods/