From 71f3e7362b1b05ce0af3ee02f2613bedf19338af Mon Sep 17 00:00:00 2001 From: Arek W Date: Tue, 20 Dec 2016 09:55:13 +1100 Subject: [PATCH] Fix hasmany 'has' accessor to work with custom keys --- Changelog.md | 3 +++ lib/Associations/Many.js | 12 +++++++++--- package.json | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Changelog.md b/Changelog.md index 35983490..accaffbc 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,6 @@ +### v3.2.2 +- Fix the fix in [#761](../../issues/761) so that it works with custom keys ([#762](../../issues/762)) + ### v3.2.1 - Fix has many 'has' accessor failing when join table has duplicate entries ([#761](../../issues/761)) diff --git a/lib/Associations/Many.js b/lib/Associations/Many.js index 564659ce..29230899 100644 --- a/lib/Associations/Many.js +++ b/lib/Associations/Many.js @@ -185,10 +185,16 @@ function extendInstance(Model, Instance, Driver, association, opts, createInstan if (err) return cb(err); if (_.isEmpty(Instances)) return cb(null, false); - var foundItemsIDs = _(foundItems).map('id').uniq().value(); - var InstancesIDs = _(Instances ).map('id').uniq().value(); + var mapKeysToString = function (item) { + return _.map(association.model.keys, function (k) { + return item[k]; + }).join(',') + } + + var foundItemsIDs = _(foundItems).map(mapKeysToString).uniq().value(); + var InstancesIDs = _(Instances ).map(mapKeysToString).uniq().value(); - var sameLength = foundItemsIDs.length == InstancesIDs.length; + var sameLength = foundItemsIDs.length == InstancesIDs.length; var sameContents = sameLength && _.isEmpty(_.difference(foundItemsIDs, InstancesIDs)); return cb(null, sameContents); diff --git a/package.json b/package.json index 1403d501..850fdd73 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "sqlite", "mongodb" ], - "version" : "3.2.1", + "version" : "3.2.2", "license" : "MIT", "homepage" : "http://dresende.github.io/node-orm2", "repository" : "http://github.com/dresende/node-orm2.git",