balderdashy/sails — pull request #1439 by ndhoule (2014-02-24 03:41:42+00:00)
Title: Fix pubsub hook erroneously broadcasting when association is not populated

Creating a model with a one-to-one association triggers a [pubsub update on one-to-one associations](https://github.com/balderdashy/sails/blob/master/lib/hooks/pubsub/index.js#L1076), but [doesn't account for the case where the association isn't yet populated](https://github.com/balderdashy/sails/blob/master/lib/hooks/pubsub/index.js#L668); i.e., the association is `null`.

```javascript
// api/models/User.js
module.exports = {
attributes: {
username: 'string',

profile: {
model: 'profile',
via: 'user'
}
// ...
}
};

// api/models/Profile.js
module.exports = {
attributes: {
user: {
model: 'user',
via: 'profile'
}
// ...
}
};

// Client code--this causes an error:
$.ajax({
method: 'POST',
url: '/user',
data: {
username: 'test_user'
}
// ...
});
```

The error message:
```javascript
/Users/ndhoule/ephemeral/sails-testbed/app/node_modules/sails/lib/hooks/pubsub/index.js:670
throw new Error(
^
Error: Invalid usage of `profile.publishUpdate(id, changes, [socketToOmit])`
at bound.publishUpdate (/Users/ndhoule/ephemeral/sails-testbed/app/node_modules/sails/lib/hooks/pubsub/index.js:670:12)
at bound [as publishUpdate] (/Users/ndhoule/ephemeral/sails-testbed/app/node_modules/sails/node_modules/lodash/dist/lodash.js:729:21)
at bound. (/Users/ndhoule/ephemeral/sails-testbed/app/node_modules/sails/lib/hooks/pubsub/index.js:1079:25)
at /Users/ndhoule/ephemeral/sails-testbed/app/node_modules/sails/node_modules/lodash/dist/lodash.js:901:23
at forOwn (/Users/ndhoule/ephemeral/sails-testbed/app/node_modules/sails/node_modules/lodash/dist/lodash.js:2105:15)
at Function.forEach (/Users/ndhoule/ephemeral/sails-testbed/app/node_modules/sails/node_modules/lodash/dist/lodash.js:3302:9)
at bound.publishCreate (/Users/ndhoule/ephemeral/sails-testbed/app/node_modules/sails/lib/hooks/pubsub/index.js:1055:7)
at bound [as publishCreate] (/Users/ndhoule/ephemeral/sails-testbed/app/node_modules/sails/node_modules/lodash/dist/lodash.js:729:21)
at created (/Users/ndhoule/ephemeral/sails-testbed/app/node_modules/sails/lib/hooks/blueprints/actions/create.js:58:10)
at _normalizeCallback.callback.success (/Users/ndhoule/ephemeral/sails-testbed/app/node_modules/sails/node_modules/waterline/node_modules/node-switchback/lib/normalize.js:33:26)
```

See https://github.com/ndhoule/sails-broadcast-bug-demo for a runnable repro case. Oddly, I was able to repro this reliably using the sails-postgresql adapter, but not with the sails-disk adapter.