Skip to content

Loading…

Bug: loading child records with belongsTo relationship doesn't update inverse hasMany #479

Closed
wants to merge 1 commit into from

3 participants

@go1dfish

When loading child record containing a belongsTo relationship, its parent hasMany relationship should be updated to include the child record.

This commit introduces a failing test for this scenario

Alex Goodwin Add failing test: when loading child record containing a belongsTo re…
…lationship, its parent hasMany relationship should be updated to include the child record
168aa06
@elliterate

How does the child come to be associated with the parent? If that relationship exists prior to the loading of either comment, the store seems to assume the relationship will actually be declared by both, like so:

store.load(Comment, { id: 1, body: "parent", comments: [ 2 ] });
store.load(Comment, { id: 2, body: "child", comment_id: 1 });

If you need to specify it in only one of the loads, this might actually be a feature request. Unless, of course, that used to work at some point in the past.

@go1dfish

This is assuming the adapter/api doesn't include inline hasMany associations. It isn't made clear anywhere that this is a requirement of the adapter (to return the relationship from both sides). If this is intended to be required than a doc change would fix the bug and move it more into the realm of a feature request like you suggest

@wagenet
Ember.js member

Closing since we've redone a lot of association stuff sine this was submitted.

@wagenet wagenet closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 16, 2012
  1. Add failing test: when loading child record containing a belongsTo re…

    Alex Goodwin committed
    …lationship, its parent hasMany relationship should be updated to include the child record
Showing with 24 additions and 1 deletion.
  1. +24 −1 packages/ember-data/tests/integration/associations_test.js
View
25 packages/ember-data/tests/integration/associations_test.js
@@ -4,7 +4,9 @@ var store, adapter, Comment;
module("Associations", {
setup: function() {
- adapter = DS.Adapter.create();
+ adapter = DS.Adapter.create({
+ serializer: DS.RESTSerializer
+ });
store = DS.Store.create({
isDefaultStore: true,
@@ -26,6 +28,27 @@ module("Associations", {
}
});
+
+test("when loading child record containing a belongsTo relationship, its parent hasMany relationship should be updated to include the child record", function() {
+ // Changes to one side should not require run-loop synchronization to
+ // propagate to the other side
+ Ember.run(function() {
+ store.load(Comment, { id: 1, body: "parent" });
+ var parent = store.find(Comment, 1);
+
+ equal(parent.get('comments.length'), 0, "precond - the parent has no child comments yet");
+
+ store.load(Comment, { id: 2, body: "child", comment_id: 1 });
+
+ var child = store.find(Comment, 2);
+
+ equal(child.get('comment'), parent, 'The belongsTo relationship should be set');
+
+
+ deepEqual(parent.get('comments').toArray(), [ child ] , "there should be a child comment");
+ });
+});
+
test("when modifying a child record's belongsTo relationship, its parent hasMany relationships should be updated", function() {
// Changes to one side should not require run-loop synchronization to
// propagate to the other side
Something went wrong with that request. Please try again.