flatiron/resourceful — Issue #42: createParent() does not appear to update parent object with new child id
Author: brynbellomy
Created: 2012-01-07 16:10:04+00:00
State: open

Only tested this with couch, not the memory store.

So -- having defined two resources, "parent" and "child", and having called Parent.child('child') and/or Child.parent('parent'), I'm finding that when I then call Parent.createChild(), the child object in the database is saved with the parent object's id, but the parent object is not updated with the new child object's id.

Given that I haven't looked at any of this code until an hour or two ago, I wonder if someone would scan over my proposed fix before I issue a formal pull request just to make sure that I'm on the same page and am not completely missing your intent. Here's the code:

Beginning on line 397 of resource.js...

//
// Parent.createChild(id, child, callback)
//
factory['create' + rstringc] = function (id, child, callback) {
var key = factory.resource.toLowerCase() + '_id';
var that = this
var updateParent = function(err, savedChild) {
factory.get(id, function(err, parentObject) {
if (err) {
callback(err)
} else if (parentObject) {
// add the new child id onto the parent object
parentObject[rstring + '_ids'].push(savedChild._id || savedChild.id)
parentObject.save(function(err) {
callback.call(this, err, savedChild)
})
} else {
throw new Error("could not find parent object")
}
})
}
if (child instanceof rfactory) {
child[key] = id;
child.save(updateParent);
} else {
var inheritance = {};
inheritance[key] = id;
child = resourceful.mixin({}, child, inheritance);
rfactory.create(child, updateParent);
}
};