Skip to content

Commit 704bc96

Browse files
committed
Find on hasMany scope method
1 parent f56cbaa commit 704bc96

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/model.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ AbstractClass.hasMany = function hasMany(anotherClass, params) {
938938
anotherClass.find(id, function (err, inst) {
939939
if (err) return cb(err);
940940
if (!inst) return cb(new Error('Not found'));
941-
if (inst[fk] == this.id) {
941+
if (inst[fk] && inst[fk].toString() == this.id.toString()) {
942942
cb(null, inst);
943943
} else {
944944
cb(new Error('Permission denied'));

test/relations.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ describe('relations', function() {
9292
});
9393
}
9494
});
95+
96+
it('should find scoped record', function(done) {
97+
var id;
98+
Book.create(function(err, book) {
99+
book.chapters.create({name: 'a'}, function(err, ch) {
100+
id = ch.id;
101+
book.chapters.create({name: 'z'}, function() {
102+
book.chapters.create({name: 'c'}, function() {
103+
fetch(book);
104+
});
105+
});
106+
});
107+
});
108+
109+
function fetch(book) {
110+
book.chapters.find(id, function(err, ch) {
111+
should.not.exist(err);
112+
should.exist(ch);
113+
ch.id.should.equal(id);
114+
done();
115+
});
116+
}
117+
});
95118
});
96119

97120
describe('belongsTo', function() {

0 commit comments

Comments
 (0)