Edit Page

Add (Blueprint)

Add a foreign record (e.g. a comment) to one of this record's collection associations (e.g. "comments").

POST /:model/:id/:association/:fk

This action pushes a reference to some other record (the "foreign" record) onto a collection attribute of this record (the "primary" record).

  • If :fk of an existing foreign record is supplied, it will be associated with the primary record.
  • If no :fk is supplied, and the body of the POST contains values for a new record, that record will be created and associated with the primary record.
  • If the collection association within the primary record already contains a reference to the foreign record, this action will be ignored.
  • If the association is 2-way (i.e. reflexive, with "via" on both sides) the association on the foreign record will also be updated.

Parameters

Parameter Type Details
model The identity of the containing model for the parent record.

e.g. 'employee' (in /employee/7/involvedinPurchases/47)
id The desired target record's primary key value

e.g. '7' (in /employee/7/involvedInPurchases/47)
association The name of the collection association

e.g. 'involvedInPurchases'
fk The primary key (e.g. id) of the foreign record to add to this collection association.

e.g. 47
callback If specified, a JSONP response will be sent (instead of JSON). This is the name of the client-side javascript function to call, passing results as the first (and only) argument

e.g. ?callback=myJSONPHandlerFn

Example

Add purchase #47 to the list of purchases that Dolly (employee #7) has been involved in:

POST /employee/7/involvedInPurchases/47

Run in Postman

Expected response

This returns "Dolly", the parent record. Notice she is now involved in purchase #47:

{
  "id": 7,
  "createdAt": "2014-08-03T01:16:35.440Z",
  "name": "Dolly",
  "updatedAt": "2014-08-03T01:51:41.567Z",
  "involvedInPurchases": [
    {
      "amount": 10000,
      "createdAt": "2014-08-03T01:50:33.898Z",
      "updatedAt": "2014-08-03T01:51:08.227Z",
      "id": 47,
      "cashier": 7
    }
  ]
}
Using jQuery
$.post('/employee/7/involvedInPurchases/47', function (purchases) {
  console.log(purchases);
});
Using Angular
$http.post('/employee/7/involvedInPurchases/47')
.then(function (purchases) {
  console.log(purchases);
});
Using sails.io.js
io.socket.post('/employee/7/involvedInPurchases/47', function (purchases) {
  console.log(purchases);
});
Using cURL
curl http://localhost:1337/employee/7/involvedInPurchases/47 -X "POST"

Notes

  • If you'd like to spend some more time with Dolly, a more detailed walkthrough related to the example above is available here.
  • This action is for dealing with plural ("collection") associations. If you want to set or unset a singular ("model") association, just use update and set the model association to the id of the new foreign record (or null to clear the association).
  • The example above assumes "rest" blueprints are enabled, and that your project contains at least an 'Employee' model with association: involvedInPurchases: {collection: 'Purchase', via: 'cashier'} as well as a Purchase model with association: cashier: {model: 'Employee'}. You'll also need at least an empty PurchaseController and EmployeeController. You can quickly achieve this by running:

    $ sails new foo
    $ cd foo
    $ sails generate api purchase
    $ sails generate api employee
    

...then editing api/models/Purchase.js and api/models/Employee.js.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Sails logo
  • Home
  • Get started
  • Support
  • Documentation
  • Documentation

For a better experience on sailsjs.com, update your browser.

Documentation

Reference Concepts App structure | Upgrading Contribution guide | Tutorials More

Reference

  • Application
    • Events
    • Lifecycle
    • sails.getRouteFor()
    • sails.getUrlFor()
    • sails.lift()
    • sails.load()
    • sails.log()
    • sails.lower()
    • sails.request()
    • sails.getBaseUrl()
  • Blueprint API
    • add to
    • create
    • destroy
    • find one
    • find where
    • populate where
    • remove from
    • update
  • Command Line Interface
    • sails console
    • sails debug
    • sails generate
    • sails lift
    • sails new
    • sails version
  • Configuration
    • sails.config.*
    • sails.config.blueprints
    • sails.config.bootstrap()
    • sails.config.connections
    • sails.config.cors
    • sails.config.csrf
    • sails.config.globals
    • sails.config.http
    • sails.config.i18n
    • sails.config.log
    • sails.config.models
    • sails.config.policies
    • sails.config.routes
    • sails.config.session
    • sails.config.sockets
    • sails.config.views
  • Request (`req`)
    • req.accepted
    • req.acceptedCharsets
    • req.acceptedLanguages
    • req.body
    • req.cookies
    • req.fresh
    • req.headers
    • req.host
    • req.ip
    • req.ips
    • req.isSocket
    • req.method
    • req.options
      • req.options.values
      • req.options.where
    • req.originalUrl
    • req.params
    • req.path
    • req.protocol
    • req.query
    • req.secure
    • req.signedCookies
    • req.socket
    • req.subdomains
    • req.url
    • req.wantsJSON
    • req.xhr
    • req.accepts()
    • req.acceptsCharset()
    • req.acceptsLanguage()
    • req.allParams()
    • req.file()
    • req.get()
    • req.is()
    • req.param()
  • Response (`res`)
    • res.attachment()
    • res.badRequest()
    • res.clearCookie()
    • res.cookie()
    • res.created()
    • res.forbidden()
    • res.get()
    • res.json()
    • res.jsonp()
    • res.location()
    • res.negotiate()
    • res.notFound()
    • res.ok()
    • res.redirect()
    • res.send()
    • res.serverError()
    • res.set()
    • res.status()
    • res.type()
    • res.view()
  • Waterline (ORM)
    • Models
      • .count()
      • .create()
      • .destroy()
      • .find()
      • .findOne()
      • .findOrCreate()
      • .native()
      • .query()
      • .stream()
      • .update()
    • Populated Values
      • .add()
      • .remove()
    • Queries
      • .exec()
      • .limit()
      • .populate()
      • .skip()
      • .sort()
      • .where()
    • Records
      • .save()
      • .toJSON()
      • .toObject()
  • WebSockets
    • Resourceful PubSub
      • .message()
      • .publishAdd()
      • .publishCreate()
      • .publishDestroy()
      • .publishRemove()
      • .publishUpdate()
      • .subscribe()
      • .unsubscribe()
      • .unwatch()
      • .watch()
      • .subscribers()
    • sails.sockets
      • .addRoomMembersToRooms()
      • .blast()
      • .broadcast()
      • .getId()
      • .join()
      • .leave()
      • .leaveAll()
      • .removeRoomMembersFromRooms()
      • sails.sockets.emit()
      • sails.sockets.id()
      • sails.sockets.rooms()
      • sails.sockets.socketRooms()
      • sails.sockets.subscribers()
    • Socket Client
      • io.sails
      • io.socket
      • SailsSocket
        • Methods
        • Properties
      • io.socket.delete()
      • io.socket.get()
      • io.socket.off()
      • io.socket.on()
      • io.socket.post()
      • io.socket.put()
      • io.socket.request()

Built with Love

The Sails framework is maintained by a web & mobile studio in Austin, TX, with the help of our contributors. We created Sails in 2012 to assist us on Node.js projects. Naturally we open-sourced it. We hope it makes your life a little bit easier!

Sails:
  • What is Sails?
  • Treeline IDE
  • Contribute
  • Logos/artwork
About:
  • The Sails Company
  • Security
  • News
  • Legal
Help:
  • Get started
  • Documentation
  • Docs
  • Enterprise
  • Hire us

© 2012-2018 The Sails Company. 
The Sails framework is free and open-source under the MIT License.