Merge pull request #739 from mdartic/master

Fix filtering relations of a model with an order specified
This commit is contained in:
Raymond Feng 2015-10-28 08:38:03 -07:00
commit d0072b68a1
2 changed files with 34 additions and 1 deletions

View File

@ -917,8 +917,13 @@ DataAccessObject.findByIds = function(ids, query, options, cb) {
var pk = idName(this);
filter.where[pk] = { inq: [].concat(ids) };
mergeQuery(filter, query || {});
// to know if the result need to be sorted by ids or not
// this variable need to be initialized before the call to find, because filter is updated during the call with an order
var toSortObjectsByIds = filter.order ? false : true ;
this.find(filter, options, function(err, results) {
cb(err, err ? results : utils.sortObjectsByIds(pk, ids, results));
cb(err, toSortObjectsByIds ? utils.sortObjectsByIds(pk, ids, results) : results );
});
return cb.promise;
};

View File

@ -4996,6 +4996,19 @@ describe('relations', function () {
});
});
it('should find items on scope and ordered them by name DESC', function(done) {
Category.find(function(err, categories) {
categories.should.have.length(1);
categories[0].jobs({order: 'name DESC'}, function (err, jobs) {
should.not.exist(err);
jobs.should.have.length(2);
jobs[0].id.should.eql(job3.id)
jobs[1].id.should.eql(job2.id)
done();
})
});
});
it('should allow custom scope methods - reverse', function(done) {
Category.findOne(function(err, cat) {
cat.jobs.reverse(function(err, ids) {
@ -5254,6 +5267,21 @@ describe('relations', function () {
.catch(done);
});
it('should find items on scope and ordered them by name DESC', function (done) {
Category.find()
.then(function (categories) {
categories.should.have.length(1);
return categories[0].jobs.getAsync({order: 'name DESC'})
})
.then(function (jobs) {
jobs.should.have.length(2);
jobs[0].id.should.eql(job3.id);
jobs[1].id.should.eql(job2.id);
done();
})
.catch(done);
});
it('should allow custom scope methods with promises - reverse', function(done) {
Category.findOne()
.then(function (cat) {