Populate and return foreign record(s) for the given association of this record.
GET /:model/:id/:association
If the specified association is plural ("collection"), this action returns the list of associated records as a JSON-encoded array of dictionaries (plain JavaScript objects). If the specified association is singular ("model"), this action returns the associated record as a JSON-encoded dictionary.
Parameter | Type | Details |
---|---|---|
model | The identity of the containing model. e.g. 'purchase' (in GET /purchase/47/cashier ) |
|
id | The primary key of the parent record. e.g. '47' (in GET /purchase/47/cashier ) |
|
association | The name of the association. e.g. 'cashier' (in GET /purchase/47/cashier ) or 'products' (in GET /purchase/47/products ) |
Populate the cashier
who conducted purchase #47:
GET /purchase/47/cashier
{
"name": "Dolly",
"id": 7,
"createdAt": "2012-05-14T01:21:05.000Z",
"updatedAt": "2013-01-15T01:18:40.000Z"
}
Using jQuery:
$.get('/purchase/47/cashier', function (cashier) {
console.log(cashier);
});
Using Angular:
$http.get('/purchase/47/cashier')
.then(function (cashier) {
console.log(cashier);
});
Using sails.io.js:
io.socket.get('/purchase/47/cashier', function (cashier) {
console.log(cashier);
});
Using cURL:
curl http://localhost:1337/purchase/47/cashier
The example above assumes "rest" blueprints are enabled, and that your project contains at least an empty 'Employee' model as well as a
Purchase
model with an association attribute:cashier: {model: 'Employee'}
. You'll also need at least an emptyPurchaseController
andEmployeeController
. 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
.