dresende/node-orm2 — pull request #479 by Kreees (2014-03-25 10:06:18+00:00)
Title: Wrong createInstance in hasMany associations. MongoDB.

Hello.

In case of hasMany association in return of instance `getAccessor` (ex. getPets) you get instances of wrong models with MongoDB driver. In `Many.js` module `extendInstance` function (within createInstance call) utilizes `Driver.hasMany` on mongodb driver.

###### /lib/Association/Many.js
~~~js

206: if (Driver.hasMany) {
207:	return Driver.hasMany(Model, association).get(Instance, conditions, options, createInstance, cb);
208: }
~~~

But passed `createInstance` argument is supposed for current model instance creation, not for associated model. Hence all associated objects become wrapped in wrong model class.

That case is swallowed by test due to checks are accomplished by presence and `name` property presented in both models (Person and Pet)

###### /test/integration/association-hasmany.js
~~~js
18:	Person = db.define('person', {
19:	name : String,
20:	surname : String,
21:	age : Number
22:	});
23:	Pet = db.define('pet', {
24:	name : String
25:	});
...
77: people[0].getPets("-name", function (err, pets) {
...
82:	pets[0].name.should.equal("Mutt");
~~~

At the same time everything works fine with SQL drivers (checked with sqlite).

I would have made a pool request, but in that case `createInstance` definition in `/ib/Model.js` is hidden by closure and not accessible directly from model.