Skip to content

Instantly share code, notes, and snippets.

@JemiloII
Created December 16, 2014 17:15
Show Gist options
  • Save JemiloII/89ab93f65ea0c481be4a to your computer and use it in GitHub Desktop.
Save JemiloII/89ab93f65ea0c481be4a to your computer and use it in GitHub Desktop.
Is this possible to do in sails? Reference a model from another model.
/**
* Bar.js
*
* @description :: Model for the Bar Collection
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = {
attributes: {
name: 'string'
}
};
/**
* Foo.js
*
* @description :: Model for the Foo Collection
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = {
attributes: {
baz: function(params){
bar.findOrCreate(params).exec(function(err, data){
if(err){ return err; }
return data;
});
}
}
};
/**
* FooController
*
* @description :: Server-side logic for managing foos
* @help :: See http://links.sailsjs.org/docs/controllers
*/
module.exports = {
create: function(req, res){
// baz is an object
Foo.create({baz: req.body.baz}).exec(function(err, data){
if(err){ return res.serverError(err); }
return res.json(data);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment