问题与SequelizeJS和关系

时间:2014-09-11 13:03:04

标签: sequelize.js

我想根据相关模型的参数更改模型中的值...

我不知道如何在我的预约模式中获得客户的性别......

var Appointment = db.define('Appointment', {
    title: {
        type: Types.STRING,
        validate: {
            len: {
                args: [1, 50],
                msg: 'Vous devez saisir un titre entre 1 et 50 caractères.'
            }
        }
    },
    start: {
        type: Types.DATE
    },
    end: {
        type: Types.DATE
    },
    backgroundColor: {
        type: Types.STRING,
        defaultValue: 'red'
        set: function(value){
            // if Customer.gender === 'M' >> this.backgroundColor = 'red'
            // if Customer.gender === 'F' >> this.backgroundColor = 'yellow'
        }
    }
}, {
    classMethods:{
        associate: function (models) {
            Appointment.belongsTo(models.Customer, {as: 'Customer'});
        }
    }
});

1 个答案:

答案 0 :(得分:0)

你可以使用:

Appointment.find({ where : {id :1},
                   include  : User })
.success(function(appointment){
    console.log(appointment); // visualize your instance
    appointment.user.gender;
});

更多信息:https://github.com/sequelize/sequelize/wiki/API-Reference-Model#findAll 寻找选项[options.include]

也许是一个很好的答案:sequelize fetching associations on find (1.6)