-1

I have a model where only one record can have a 'current' property set to, for example, 1.

Is it possible for the beforeCreate or beforeUpdate to access the collection. Basically I want to do something like this:

afterUpdate: function (values, next) {
    // If this value is current, reset all the others
    if (values.current == 1) {
        this.collection.update({
            id: { '!': values.id }
        }, {
            current: 0
        }, next);
    }
}

What id don't know is what I can reliably use for this.collection in the example above.

Thanks in advance.

Andrew Eddie
  • 988
  • 6
  • 15

1 Answers1

0

Yes, if you're using Sails v0.10.X you can access any model using sails global variable:

sails.models.users.find({id: 1})

Note: all model names in sails.models are in lower case

abeja
  • 603
  • 4
  • 5
  • I'm not using sails. I'm using Waterline standalone. I guess I could use a factory pattern to put the Waterline object in the scope of the model, but I'd prefer to avoid that. It does seem reasonable that `this` of the event handlers should be the model (it appears to be the global context). – Andrew Eddie Aug 31 '14 at 04:42