diff --git a/lib/Drivers/DML/mongodb.js b/lib/Drivers/DML/mongodb.js index 45fc3657..433e97e1 100644 --- a/lib/Drivers/DML/mongodb.js +++ b/lib/Drivers/DML/mongodb.js @@ -256,21 +256,24 @@ Driver.prototype.hasMany = function (Model, association) { } conditions._id = { $in: [] }; - options.extra = {}; + var extra = {}; for (var i = 0; i < docs[0][association.name].length; i++) { conditions._id.$in.push(new mongodb.ObjectID(docs[0][association.name][i]._id)); - options.extra[docs[0][association.name][i]._id] = docs[0][association.name][i]; + extra[docs[0][association.name][i]._id] = docs[0][association.name][i]; } if (options.order) { options.order[0] = options.order[0][1]; - options.order = Utilities.standardizeOrder(options.order); } - options.extra_props = association.props; - options.createInstance = createInstance; - - return driver.find(null, association.model.table, conditions, options, cb); + return association.model.find(conditions,options,function(e,docs){ + var i, len; + for(i = 0, len = docs.length; i < len; i++){ + if (extra.hasOwnProperty(docs[i][association.model.id])) + docs[i].extra = extra[docs[i][association.model.id]]; + } + cb(e,docs); + }); }); }, add: function (Instance, Association, data, cb) { diff --git a/test/integration/association-hasmany.js b/test/integration/association-hasmany.js index 27b2a4ed..9e3f1a65 100644 --- a/test/integration/association-hasmany.js +++ b/test/integration/association-hasmany.js @@ -79,6 +79,7 @@ describe("hasMany", function () { should(Array.isArray(pets)); pets.length.should.equal(2); + pets[0].model().should.equal(Pet); pets[0].name.should.equal("Mutt"); pets[1].name.should.equal("Deco"); @@ -87,6 +88,15 @@ describe("hasMany", function () { }); }); + it ("should return proper instance model", function(done){ + Person.find({ name: "John" }, function (err, people) { + people[0].getPets("-name", function (err, pets) { + pets[0].model().should.equal(Pet); + return done(); + }); + }); + }); + it("should allow to specify order as Array", function (done) { Person.find({ name: "John" }, function (err, people) { should.equal(err, null); diff --git a/test/integration/association-hasone.js b/test/integration/association-hasone.js index af3d1529..d676064b 100644 --- a/test/integration/association-hasone.js +++ b/test/integration/association-hasone.js @@ -72,6 +72,15 @@ describe("hasOne", function() { }); }); + it("should return proper instance model", function (done) { + Leaf.one({ size: 14 }, function (err, leaf) { + leaf.getTree(function (err, tree) { + tree.model().should.equal(Tree); + return done(); + }); + }); + }); + it("get should get the association with a shell model", function (done) { Leaf(leafId).getTree(function (err, tree) { should.not.exist(err);