Populate Where…

If the specified association is plural (“collection”), this action returns the list of associated records as a JSON array of objects. If the specified association is singular (“model”), this action returns the associated record as a JSON object.

  1. GET /:model/:record/:association

Example

Populate the cashier who conducted purchase #47.

Using jQuery:

  1. $.get('/purchase/47/cashier', function (purchase) {
  2. console.log(purchase);
  3. });

Using Angular:

  1. $http.get('/purchase/47/cashier')
  2. .then(function (purchase) {
  3. console.log(purchase);
  4. });

Using sails.io.js:

  1. io.socket.get('/purchase/47/cashier', function (purchase) {
  2. console.log(purchase);
  3. });

Using cURL:

  1. curl http://localhost:1337/purchase/47/cashier

Should return

  1. {
  2. "amount": 99.99,
  3. "id": 47,
  4. "cashier": {
  5. "name": "Dolly",
  6. "id": 7,
  7. "createdAt": "2012-05-14T01:21:05.000Z",
  8. "updatedAt": "2013-01-15T01:18:40.000Z"
  9. },
  10. "createdAt": "2013-10-14T01:22:00.000Z",
  11. "updatedAt": "2013-10-15T01:20:54.000Z"
  12. }

Notes

  • 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 empty PurchaseController and EmployeeController. You can quickly achieve this by running:

    1. $ sails new foo
    2. $ cd foo
    3. $ sails generate api purchase
    4. $ sails generate api employee

…then editing api/models/Purchase.js.