From 704bc965bd557f6522574aef913b4cf149958dbf Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Thu, 4 Apr 2013 00:46:41 +0400 Subject: [PATCH] Find on hasMany scope method --- lib/model.js | 2 +- test/relations.test.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index 988ad6f1..9fbcee74 100644 --- a/lib/model.js +++ b/lib/model.js @@ -938,7 +938,7 @@ AbstractClass.hasMany = function hasMany(anotherClass, params) { anotherClass.find(id, function (err, inst) { if (err) return cb(err); if (!inst) return cb(new Error('Not found')); - if (inst[fk] == this.id) { + if (inst[fk] && inst[fk].toString() == this.id.toString()) { cb(null, inst); } else { cb(new Error('Permission denied')); diff --git a/test/relations.test.js b/test/relations.test.js index c5c405a8..5628cd57 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -92,6 +92,29 @@ describe('relations', function() { }); } }); + + it('should find scoped record', function(done) { + var id; + Book.create(function(err, book) { + book.chapters.create({name: 'a'}, function(err, ch) { + id = ch.id; + book.chapters.create({name: 'z'}, function() { + book.chapters.create({name: 'c'}, function() { + fetch(book); + }); + }); + }); + }); + + function fetch(book) { + book.chapters.find(id, function(err, ch) { + should.not.exist(err); + should.exist(ch); + ch.id.should.equal(id); + done(); + }); + } + }); }); describe('belongsTo', function() {