title: Discrepancy between Data in save() callback and Database
body: <p>I am working on a multiplayer card game in sails.js and am running into an issue concerning save(). I am querying for 3 records, making changes to their a few of their association attributes, then saving them sequentially. The problem is that the temp copy of the saved data passed as a parameter to the function passed to save(cb), is different than the information on the database.</p>
  
  <p>I am using two relevant models: Cards, which have an 'attachments' attribute, that is a collection of card records, and Players, which have 'hand' and 'runes' attributes, which are both collections of card records.</p>
  
  <p>The action takes the following parameters:</p>
  
  <p>req.body.thiefId and req.body.victimId are the id's of Player records.
  req.body.jackId and req.body.targetId are the id's of Card records.</p>
  
  <p>req.body.pNum is the index of the thief player after the players are sorted. sortPlayers[req.body.pNum] is the thief player and sortPlayers[(req.body.pNum + 1) % 2] is the victim player.</p>
  
  <p>The action takes a jack out of the thief's hand, puts it into the attachments collection of the target card (which begins in the victim player's runes collection), removes the target card from the victim player's runes collection and adds the target card to the thief player's runes collection. Then the records are all saved. Essentially, the jack is played from one player's (the thief's) hand, 'on top' of a card in the victim player's runes, and then the target card is moved to the thief's runes.</p>
  
  <p>The problem is that the 'runes' attributes of both players are not being properly persisted on the database. Whichever player is the victim keeps the target card in their runes collection and the the thief player does not add the target card to their runes collection. The jack card is properly removed from the hand collection of the thief player and added to the attachments of the target card. What is really odd is that the local copy of the data returned by save() is correct! The records savedP0 and savedP1 always have the correct rune collections, but checking the server at localhost:1337/player reveals that the database has not persisted the changes. How is this possible? My understanding of the save() method is that the record parameter passed to the function that is passed to save() is always an exact copy of what the database actually recorded. </p>
  
  <p>Here is a relatively minimalistic example of the action causing the problem:</p>
  
  <pre><code>    jackBug: function (req, res) {
          Player.find([req.body.thiefId, req.body.victimId]).populateAll().exec(function (erro, players) {
              Card.findOne(req.body.targetId).populate('attachments').exec(function (err, targetCard) {
                  //Sort the players according to their 'pNum' attribute
                  var playerSort = sortPlayers(players);
  
                  //Remove the jack from the theif player's hand and add the target card to the thief player's points
                  playerSort[req.body.pNum].runes.add(targetCard.id);
                  playerSort[req.body.pNum].hand.remove(req.body.jackId);
  
                  //Remove the target card from the victim player's points
                  playerSort[(req.body.pNum + 1) % 2].runes.remove(targetCard.id);
  
                  //Add the jack card to the target card's attachments
                  targetCard.attachments.add(req.body.jackId);
  
  
                      playerSort[0].save(function (e, savedP0) {
                          playerSort[1].save(function (e6, savedP1) {
                              targetCard.save(function(e7, savedTarget) {
                                  console.log("\nsavedP0:");
                                  //This data is always correct, even though the server is wrong
                                  console.log(savedP0);
                                  console.log("\n\nsavedP1");
                                  //This data is also always correct, but it also disagrees with the server
                                  console.log(savedP1);
                              });
                          });
                      });
              });
      });
  },
  </code></pre>
  
  <p>What is happening? How can the database disagree with the record passed to the save() cb?</p>
  
  <p>EDIT: Here is a refactored version of the action, which does not make a call to sortPLayer(players), in the hopes of avoiding issues with asynchronicity. The records returned to the cb's passed to save()'s are still correct and the database is still wrong.</p>
  
  <pre><code>    jackBug: function (req, res) {
          Player.find([req.body.thiefId, req.body.victimId]).populateAll().exec(function (erro, players) {
              Card.findOne(req.body.targetId).populate('attachments').exec(function (err, targetCard) {
  
                  if (players[0].id === req.body.thiefId) {
                      var thiefIndex = 0;
                      var victimIndex = 1;
                  } else if (players[1].id === req.body.thiefId) {
                      var thiefIndex = 1;
                      var victimIndex = 0;
                  }
  
                  //Remove the jack from the theif player's hand and add the target card to the thief player's points
                  players[thiefIndex].runes.add(targetCard.id);
                  players[thiefIndex].hand.remove(req.body.jackId);
  
                  //Remove the target card from the victim player's runes
                  players[victimIndex].runes.remove(targetCard.id);
  
                  //Add the jack card to the target card's attachments
                  targetCard.attachments.add(req.body.jackId);
  
  
                      players[0].save(function (e, savedP0) {
                          players[1].save(function (e6, savedP1) {
                              targetCard.save(function(e7, savedTarget) {
                                  console.log("\nsavedP0:");
                                  console.log(savedP0);
                                  console.log("\n\nsavedP1");
                                  console.log(savedP1);
                              });
                          });
                      });
              });
      });
  },
  </code></pre>