diff --git a/packages/ember-data/lib/system/many-array.js b/packages/ember-data/lib/system/many-array.js index 140a0a46ded..e1337cf728d 100644 --- a/packages/ember-data/lib/system/many-array.js +++ b/packages/ember-data/lib/system/many-array.js @@ -80,7 +80,9 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, { toSet = toSet.concat(newRecords); var oldLength = this.length; this.arrayContentWillChange(0, this.length, toSet.length); - this.set('length', toSet.length); + if (!this.isDestroyed) { + this.set('length', toSet.length); + } this.currentState = toSet; this.arrayContentDidChange(0, oldLength, this.length); //TODO Figure out to notify only on additions and maybe only if unloaded diff --git a/packages/ember-data/tests/integration/relationships/has-many-test.js b/packages/ember-data/tests/integration/relationships/has-many-test.js index 7e02af05c75..e65099c1a3f 100644 --- a/packages/ember-data/tests/integration/relationships/has-many-test.js +++ b/packages/ember-data/tests/integration/relationships/has-many-test.js @@ -2340,3 +2340,65 @@ test("metadata should be reset between requests", function() { }); }); }); + +test("unloading and reloading a record with hasMany relationship", function() { + var user; + var message; + + run(function() { + env.store.push({ + data: [{ + type: 'user', + id: 'user-1', + attributes: { + name: 'Adolfo Builes' + }, + relationships: { + messages: { + data: [ + { type: 'message', id: 'message-1' } + ] + } + } + }, { + type: 'message', + id: 'message-1' + }] + }); + + user = env.store.peekRecord('user', 'user-1'); + message = env.store.peekRecord('message', 'message-1'); + + equal(get(user, 'messages.firstObject.id'), 'message-1'); + equal(get(message, 'user.id'), 'user-1'); + }); + + run(function() { + env.store.unloadRecord(user); + }); + + run(function() { + // The record is resurrected for some reason. + env.store.push({ + data: [{ + type: 'user', + id: 'user-1', + attributes: { + name: 'Adolfo Builes' + }, + relationships: { + messages: { + data: [ + { type: 'message', id: 'message-1' } + ] + } + } + }] + }); + + user = env.store.peekRecord('user', 'user-1'); + + equal(get(user, 'messages.firstObject.id'), 'message-1'); + equal(get(message, 'user.id'), 'user-1'); + }); +});