Someone asked this before (Order Bookshelf.js fetch by related column value) but the answers didn't work out for me.
Lets say I have the following models
var FirstModel = DB.Model.extend{
tableName: 'first',
idAttribute: 'id',
createdAt: 'created_at'
}
var SecondModel = DB.Model.extend{
tableName: 'second',
idAttribute: 'id',
firstModel: function(){
return this.belongsTo(FirstModel,'firstModel_id')
}
}
Inside my query I specify to order the results by created_at
SecondModel.forge().fetchAll({
withRelated: [{
'firstModel': function(qb){
qb.orderBy('created_at','asc');
}
}]
}).then(function(result){
console.log(result.toJSON());
});
Unfortunately the resulting collection isn't sorted according to my query.
Is it possible to do such a thing ?