Sails handles the join table for you, so you don't need the TagRelation
model at all:
// Post model
var Post = {
title: 'string',
body: 'string',
tags: {
collection: 'tag',
via: 'posts',
dominant: true // could be on either model, doesn't matter
}
};
// Tag model
var Tag = {
name: 'string',
posts: {
collection: 'post',
via: 'tags'
}
};
This way the blueprint /post/1
will contain all its associated tags
. See the association docs for more info - broken link fixed.
The dominant:true
tag lets Sails know which side of an association to put the join table on, in case the two models are in different databases. We're working on making this optional when the two models are in the same database, but for now it has to be specified explicitly.