I have been fighting this problem for a couple of days and just can’t figure out what I am doing wrong. I have a HABTM relationship side up in Rails and I have that set up in Ember as well. Anytime I try and add a record to the relationship, I never see any ids sent with the json.
Here is my action:
var self = this,
player = {},
game = {};
self.set('notify','');
this.set('content',this.get('session.currentUser'));
if(Ember.isEmpty(this.get('gameId'))){
game = self.store.createRecord('game');
game.save()
.then(function(){
return self.model.save();
})
.then(function(newPlayer){
player = newPlayer;
return game.get('players');
})
.then(function(players){
players.pushObject(player);
return game.save();
})
.then(function(){
self.transitionToRoute('games.game',game);
})
.catch(function(err){
// TODO Need better error handling here
console.log(err);
throw err;
});
self.model
is a Player
model. This will send a JSON request body:
{"game":{"token":"g-XxcnOo","state":0,"player_ids":[]}}
This is my serializer that I have set up:
export default DS.ActiveModelSerializer
.extend(DS.EmbeddedRecordsMixin)
.extend({
attrs: {
createdAt: { serialize: false },
updatedAt: { serialize: false },
players: { serialize: 'ids', deserialize: 'ids' },
owner: { embedded: 'always', serialize: 'id', deserialize: 'id' }
}
});
and this is my model:
export default DS.Model.extend({
token: DS.attr('string'),
state: DS.attr('number'),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date'),
players: DS.hasMany('player',{ async: true, inverse: 'games' }),
gameCards: DS.hasMany('gameCard',{ async: true }),
owner: DS.belongsTo('player',{ async: true, inverse: 'currentGame' })
});
Any help would be really appreciated.
I am using ActiveModelSerializer and Ember-Data 1.0.0-beta10
###Update 1
So I have been playing around with the order that my operations are going through in the action and I now am seeing something odd. If I do not save the game
object, just push an object to it, a player will show up, but as soon as I save the game
that player is removed.
###Update 2
The only solution I could find was to remove {async: true}
and go to embedding all my relationships. This is now working for me. However, it does give me cause for concern if Ember 2.0 is planning on going to {async: true}
in the future, not to mention the benefits of lazy loading those relationships to being with.