0

I have a simple need to add tags to patients. I followed the Sails and Waterline documentation concerning many-to-many associations, but it's failing at some point (no errors). I'm using MongoDB for data storage. Code below:

Tag Model

module.exports = {

attributes: {
  name: 'STRING',
  color: {
      type: 'STRING',
      defaultsTo: '#777777'
  },
  tagged: {
      collection: 'patient',
      via: 'tags',
      dominant: true
  }

}
};

Patient Model

module.exports = {

attributes: {
    name: 'STRING',
    tags: {
        collection: 'tag',
        via: 'tagged'
    }
}
};

And this is the controller method that tries to associate data:

module.exports = {

addToPatient: function(req, res) {
    Patient.findOne({id: req.param('patientId')}).exec(function(err, patient) {

        // Queue up a record to be inserted into the join table
        patient.tags.add(req.param('tagId'));

        // Save the user, creating the new associations in the join table
        patient.save(function(err) {});
    });

    res.send("tag assigned");
}
};

I've inspected the responses at various breaks and everything seems to be passing just fine. The patient is found. The save function shows a tag association in the patient object, but nothing is added in the database. I assume I will see either a join table being created or something in the patient/tag collections to signify an association, but I see nothing. I'm so very confused. If I do an HTTP get, I'm presented with a "tag assigned" response. What am I missing?

Matt Shultz
  • 312
  • 3
  • 10
  • 1
    Works fine for me on Mongo with your exact code, and creates a `patient_tags__tag_tagged` collection. You should move the `res.send` inside the callback for `patient.save`, though--right now it will always say "tag assigned" even if there was an error. – sgress454 Oct 21 '14 at 22:35

1 Answers1

0

Works fine for me, but you're right in the tag and patient collections, you won't see a populated field with the associations. You'll see new join collections that are created that contains the relationships like @sgress454 pointed out.

janex
  • 497
  • 4
  • 14