In sails-redis@0.10.0-rc3
querying associated models seems to behave as expected.
First, I created a basic User
model:
module.exports = {
attributes: {
name: 'string'
}
};
Second the ModelA
model with the associated User
:
module.exports = {
attributes: {
user: { model: 'User' }
}
};
Then I created a small mocha
test for checking the association query and reducing any errors introduced somewhere else (e.g. in your session handling):
var expect = require('chai').expect,
sails = require('sails'),
async = require('async');
describe('sails-mongo associations', function() {
before(function (done) {
sails.lift(function (err, sails) {
done(err);
});
});
after(function (done) {
sails.lower(function (err) {
done(err);
});
});
beforeEach(function(done) {
async.auto({
deleteUsers: function(done) {
User.destroy(done);
},
deleteModelA: function(done) {
ModelA.destroy(done);
}
}, function(err) {
done(err);
});
});
describe("#find", function() {
it("should allow to find a record using an associated model", function(done) {
User.create({ name: 'Test User' }).exec(function(err, user) {
expect(err).to.be.null;
expect(user).to.contain.key('id');
ModelA.create({ user: user.id }).exec(function(err, model) {
expect(err).to.be.null;
expect(model).to.contain.keys('id', 'user');
ModelA.find({ user: user }).populate('user').exec(function(err, model) {
expect(err).to.be.null;
expect(model).not.to.be.null.and.contain.key('user', user);
done();
});
});
});
});
});
});
The mocha test completes successfully without any error. Which probably means (as Scott Gress already assumed) your req.session.user
variable does not contain the value you expect. Internal conversion to mongos ObjectID
is working.
Cheers,
David