Edit Page

Add (blueprint)

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

PUT /:model/:id/:association/:fk

This action adds a reference to some other record (the "foreign", or "child" record) onto a particular collection of this record (the "primary", or "parent" record).

Parameters

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

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

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

e.g. 'involvedInPurchases'
fk String The primary key value (usually id) of the child record to add to this collection.

e.g. '47'

Example

#

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

PUT /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,
  "name": "Dolly",
  "createdAt": 1485462079725,
  "updatedAt": 1485476060873,
  "involvedInPurchases": [
    {
      "amount": 10000,
      "createdAt": 1485476060873,
      "updatedAt": 1485476060873,
      "id": 47,
      "cashier": 7
    }
  ]
}
Using jQuery
#
$.put('/employee/7/involvedInPurchases/47', function (purchases) {
  console.log(purchases);
});
Using Angular
#
$http.put('/employee/7/involvedInPurchases/47')
.then(function (purchases) {
  console.log(purchases);
});
Using sails.io.js
#
io.socket.put('/employee/7/involvedInPurchases/47', function (purchases) {
  console.log(purchases);
});
Using cURL
#
curl http://localhost:1337/employee/7/involvedInPurchases/47 -X "PUT"

Socket notifications

#

If you have WebSockets enabled for your app, then every client subscribed to the primary record will receive a notification in which the notification event name is the primary model identity (e.g. 'employee'), and the message has the following format:

id: <the parent record primary key value>,
verb: 'addedTo',
attribute: <the parent record collection attribute name>,
addedIds: <the now-added child records' primary key values>

For instance, continuing the example above, all clients subscribed to Dolly, aka employee #7, (except for the client making the request) would receive the following message:

{
  id: 7,
  verb: 'addedTo',
  attribute: 'involvedInPurchases',
  addedIds: [ 47 ]
}

Clients subscribed to the child record receive an additional notification:

Assuming involvedInPurchases had a via, then either updated or addedTo notifications would also be sent to any clients who were subscribed to purchase #47, the child record we just added.

If the via-linked attribute on the other side is also plural (e.g. cashiers), then another addedTo notification will be sent. Otherwise, if the via points at a singular attribute (e.g. cashier) then the updated notification will be sent.

Finally, a third notification might be sent:

If adding this purchase to Dolly's collection would "steal" it from another employee's involvedInPurchases, then any clients subscribed to that other, stolen-from employee record (e.g. Motoki, employee #12) would receive a removedFrom notification (see Blueprints > remove from.

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") attributes. If you want to set or unset a singular ("model") attribute, just use update and set the foreign key to the id of the new foreign record (or null to clear the association). If you want to completely replace the set of records in the collection with another set, use the replace blueprint.
  • The example above assumes "rest" blueprints are enabled, and that your project contains at least an 'Employee' model with attribute: involvedInPurchases: {collection: 'Purchase', via: 'cashier'} as well as a Purchase model with attribute: cashier: {model: 'Employee'}. You can quickly achieve this by running:

    $ sails new foo
    $ cd foo
    $ sails generate model purchase
    $ sails generate model 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 repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference