Remove from Collection

Removes an association between two records.

  1. DELETE /:model/:record/:association/:record_to_remove

This action removes a reference to some other record (the “foreign” record) from a collection attribute of this record (the “primary” record).

  • If the foreign record does not exist, it is created first.
  • If the collection doesn’t contain a reference to the foreign record, this action will be ignored.
  • If the association is 2-way (i.e. reflexive, with “via” on both sides) the association on the foreign record will also be updated.

Example

Remove Dolly (employee #7) from the employeesOfTheMonth list of store #16.

Using jQuery:

  1. $.delete('/store/16/employeesOfTheMonth/7', function (purchases) {
  2. console.log(purchases);
  3. });

Using Angular:

  1. $http.delete('/store/16/employeesOfTheMonth/7')
  2. .then(function (purchases) {
  3. console.log(purchases);
  4. });

Using sails.io.js:

  1. io.socket.delete('/store/16/employeesOfTheMonth/7', function (purchases) {
  2. console.log(purchases);
  3. });

Using cURL:

  1. curl http://localhost:1337/store/16/employeesOfTheMonth/7 -X "DELETE"

Should return store #16, the primary record:

  1. {
  2. "employeesOfTheMonth": [],
  3. "name": "Dolly",
  4. "createdAt": "2014-08-03T01:16:35.440Z",
  5. "updatedAt": "2014-08-03T01:51:41.567Z",
  6. "id": 16
  7. }

Notes

  • This action is for dealing with plural (“collection”) associations. If you want to set or unset a singular (“model”) association, just use update.
  • The example above assumes “rest” blueprints are enabled, and that your project contains at least an empty ‘Employee’ model as well as a Store model with association: employeesOfTheMonth: {collection: '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/Store.js.