sdepold/sequelize — pull request #200 by janmeier (2012-06-29 09:50:19+00:00)
Title: Checking whether an object is already associated

When an association is made between two schemas using hasMany, we would like to be able to check whether an object is currently associated with another. An example:

````javascript
Article = sequelize.define('Article', {
'title': Sequelize.STRING
});

Label = sequelize.define('Label', {
'text': Sequelize.STRING
});

Article.hasMany(Label);
````
We would then like to check, if a label is already associated with an article. This could be done by calling ````article.getLabels()```` and then looping through these labels to see if any of those matches the one we want to check for. But instead of manually looping through all the currently associated objects, we have added two methods for this.

When ````hasMany```` is called on the schema, the functions ````hasLabel```` and ````hasLabels```` are added. The first takes a single label, and checks whether this label is associated with the article. The result is returned through the usual way of adding a success callback, like this:

````javascript
article.hasLabel(label).success(function(result) {
if (result) {
alert('Label is already assigned!');
} else
alert('Label is not assigned!');
};
});
````

The second function (the plural version), takes an array of labels, and returns true if all of these are associated with the article.