Find on hasMany scope method

This commit is contained in:
Anatoliy Chakkaev 2013-04-04 00:46:41 +04:00
parent f56cbaa150
commit 704bc965bd
2 changed files with 24 additions and 1 deletions

View File

@ -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'));

View File

@ -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() {