Skip to content

Commit

Permalink
Fixes hasOne reversed associations not having setAccessor, fixes bug …
Browse files Browse the repository at this point in the history
…using wrong ID prop instead of the one defined in Model (#55)
  • Loading branch information
dresende committed Mar 4, 2013
1 parent 0723188 commit 9297815
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/Associations/One.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,35 @@ function extendInstance(Model, Instance, Driver, association, opts, cb) {
},
enumerable: false
});
if (!association.reversed) {
Object.defineProperty(Instance, association.setAccessor, {
value: function (OtherInstance, cb) {
Object.defineProperty(Instance, association.setAccessor, {
value: function (OtherInstance, cb) {
if (association.reversed) {
Instance.save(function (err) {
if (err) {
return cb(err);
}

OtherInstance[association.field] = Instance[Model.id];

return OtherInstance.save(cb);
});
} else {
OtherInstance.save(function (err) {
if (err) {
return cb(err);
}

Instance[association.field] = OtherInstance.id;
Instance[association.field] = OtherInstance[association.model.id];

return Instance.save(cb);
});
}

return this;
},
enumerable: false
});
return this;
},
enumerable: false
});
if (association.reversed) {
Object.defineProperty(Instance, association.delAccessor, {
value: function (cb) {
Instance[association.field] = 0;
Expand Down

0 comments on commit 9297815

Please sign in to comment.