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 ) |
|
where | Instead of filtering based on a specific attribute, you may instead choose to provide a where parameter with the WHERE piece of a Waterline criteria, encoded as a JSON string. This allows you to take advantage of contains , startsWith , and other sub-attribute criteria modifiers for more powerful find() queries. e.g. ?where={"name":{"contains":"theodore"}} |
|
limit | The maximum number of records to send back (useful for pagination). Defaults to 30. e.g. ?limit=100 |
|
skip | The number of records to skip (useful for pagination). e.g. ?skip=30 |
|
sort | The sort order. By default, returned records are sorted by primary key value in ascending order. e.g. ?sort=lastName%20ASC |
|
select | The attributes to include in each record in the result, specified as a comma-delimited list. By default, all attributes are selected. Not valid for plural (“collection”) association attributes. e.g. ?select=name,age . |
|
omit | The attributes to exclude from each record in the result, specified as a comma-delimited list. Cannot be used in conjuction with select . Not valid for plural (“collection”) association attributes.e.g. ?omit=favoriteColor,address . |
Populate the cashier
who conducted purchase #47:
`GET /purchase/47/cashier`
{
"name": "Dolly",
"id": 7,
"createdAt": 1485462079725,
"updatedAt": 1485476060873,
}
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
You can also populate a collection. For example, to populate the involvedInPurchases
of employee #7:
GET /employee/7/involvedInPurchases
[
{
"amount": 10000,
"createdAt": 1485476060873,
"updatedAt": 1485476060873,
"id": 47,
"cashier": 7
},
{
"amount": 50,
"createdAt": 1487015460792,
"updatedAt": 1487015476357,
"id": 52,
"cashier": 7
}
]
- In the first example above, if purchase #47 did not have a
cashier
(i.e.null
), then this action would respond with a 404 status code.The examples above assume "rest" blueprint routing is enabled (or that you've bound this blueprint action as a comparable custom route), and that your project contains at least an empty
Employee
model as well as aPurchase
model, and thatEmployee
has the association attribute:involvedInPurchases: {model: 'Purchase'}
and thatPurchase
hascashier: {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/Employee.js
andapi/models/Purchase.js
.