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).
:id
does not correspond with a primary record that exists in the database, this responds using res.notFound()
.:fk
does not correspond with a foreign record that exists in the database, this responds using res.notFound()
.via
), then the foreign key or collection it points to with that via
will also be updated on the foreign record.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 parent record's primary key value. e.g. '7' (in /employee/7/involvedInPurchases/47 ) |
|
association | The name of the collection attribute. e.g. 'involvedInPurchases' |
|
fk | The primary key value (usually id) of the child record to add to this collection. e.g. '47' |
Add purchase #47 to the list of purchases that Dolly (employee #7) has been involved in:
PUT /employee/7/involvedInPurchases/47
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
}
]
}
$.put('/employee/7/involvedInPurchases/47', function (purchases) {
console.log(purchases);
});
$http.put('/employee/7/involvedInPurchases/47')
.then(function (purchases) {
console.log(purchases);
});
io.socket.put('/employee/7/involvedInPurchases/47', function (purchases) {
console.log(purchases);
});
curl http://localhost:1337/employee/7/involvedInPurchases/47 -X "PUT"
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 anotheraddedTo
notification will be sent. Otherwise, if thevia
points at a singular attribute (e.g.cashier
) then theupdated
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.
- 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 aPurchase
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
andapi/models/Employee.js
.