Creating record with belongsTo relationship modeling


#1

Given the two models: Message.js and User.js, I keep getting the following error when trying to create a record with Ember.js. Any Ember.js wizards know what could be wrong with my code?

// Error: Assertion Failed: You can only add a ‘user’ record to this relationship

App.Message = DS.Model.extend({
title: DS.attr(),
body: DS.attr(),
user: DS.belongsTo('user'),
});

App.User = DS.Model.extend({
  first_name: DS.attr(),
  last_name: DS.attr(),
  email: DS.attr(),
  password: DS.attr(),
  letters: DS.hasMany('letter'),
});


App.NewMessageController = Ember.Controller.extend({
need: ['application'],
thisUserID: Ember.computed.alias('controllers.application.userID'),

actions: {
	save: function() {
        // Prints -> 'thisUserID' -- all is fine here
		console.log('The user id from new-letter is: '+this.get('thisUserID')); 

		var store = this.store;
        //Prints -> <DS.PromiseObject:ember433> -- Strange?
		console.log("Checking the store: "+store.find('user', this.get('thisUserID'))); 

		var newMessage = store.createRecord('message', {
			title: this.get('title'),
			body: this.get('body'),
		});
		newMessage.save();

		// Prints-> Error: Assertion Failed: You can only add a 'user' record to this relationship
		var user = store.find('user', this.get('thisUserID')).then(function(user) {
		  newMessage.set('user', user.id);
		});
	}
}
});

#2

Try

newMessage.set('user', user);