diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 29c48f13..220b18a8 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -919,13 +919,16 @@ HasManyThrough.prototype.create = function create(data, done) { * Add the target model instance to the 'hasMany' relation * @param {Object|ID} acInst The actual instance or id value */ -HasManyThrough.prototype.add = function (acInst, done) { +HasManyThrough.prototype.add = function (acInst, data, done) { var self = this; var definition = this.definition; var modelThrough = definition.modelThrough; var pk1 = definition.keyFrom; - var data = {}; + if (typeof data === 'function') { + done = data; + data = {}; + } var query = {}; // The primary key for the target model diff --git a/test/relations.test.js b/test/relations.test.js index ef212363..652cfb5a 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -433,6 +433,24 @@ describe('relations', function () { }); }); + it('should allow to add connection with through data', function (done) { + Physician.create({name: 'ph1'}, function (e, physician) { + Patient.create({name: 'pa1'}, function (e, patient) { + var now = Date.now(); + physician.patients.add(patient, { date: new Date(now) }, function (e, app) { + should.not.exist(e); + should.exist(app); + app.should.be.an.instanceOf(Appointment); + app.physicianId.should.equal(physician.id); + app.patientId.should.equal(patient.id); + app.patientId.should.equal(patient.id); + app.date.getTime().should.equal(now); + done(); + }); + }); + }); + }); + it('should allow to remove connection with instance', function (done) { var id; Physician.create(function (err, physician) {