0

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

hasib32
  • 835
  • 1
  • 13
  • 31

1 Answers1

0

When I use createRecord() with relationships, I manually construct each of the objects. There are a few different ways to do this:

For an example of adding elements to a hasMany relationship, see Ember.js & Ember Data: How to create record with hasMany relationship when the parent and child don't exist yet

Or here's another example when dealing with a belongsTo relationship:

var message = this.store.createRecord('message');
var user = this.store.createRecord('user');
message.set('user', user);

Ultimately, Ember may serialize this DS.Model to only have a number for the id, but internally, it needs an actual link to an actual object.

UPDATE: (credit to @Craicerjackless)

If the user does exist and you have the id:

 var user = this.store.find('user', 1).then(function(user){
    message.set('user, user);
});

This gets the user by the id "1" and then once the user has been found sets it as the user for the model.

Community
  • 1
  • 1
Josh Padnick
  • 3,157
  • 1
  • 25
  • 33
  • I just received suggested edits, but there was no way to incorporate or approve them through SO. I manually added the edit and credited the author; if there's another way to handle that, please let me know! – Josh Padnick Aug 27 '14 at 20:30