<NME> relationships.js
<BEF> 'use strict'

var _ = require('lodash')
var utils = require('./utils')
const linkRelatedModels = require(
  './utilities/relationship-utils'
).linkRelatedModels

module.exports = function (app, options) {
  // get remote methods.
  // set strong-remoting for more information
  // https://github.com/strongloop/strong-remoting
  var remotes = app.remotes()
  var id, data, model
      return next();
    };

    if (ctx.method.name !== 'updateAttributes') return next();

    id = ctx.req.params.id;
    data = options.data;
    model = utils.getModelFromContext(ctx, app);
    relationships(id, data, model);

    next();
    model = utils.getModelFromContext(ctx, app)

    relationships(model, id, data).then(() => next()).catch(err => next(err))
  })

  // for create
  remotes.after('**', function (ctx, next) {
    if (utils.shouldNotApplyJsonApi(ctx, options)) {
      return next()
    }

    if (ctx.method.name !== 'create') return next()

    if (ctx.result && ctx.result.data) {
      id = ctx.result.data.id
      data = options.data
      model = utils.getModelFromContext(ctx, app)
      relationships(model, id, data).then(() => next()).catch(err => next(err))
      return
    }

    next()
  })
}

function extractIdsFromResource (resource) {
  if (_.isArray(resource)) {
    return _.map(resource, 'id')
  }
  return _.get(resource, 'id', null)
    var serverRelation = model.relations[name];
    if (!serverRelation) return;
    var type = serverRelation.type;
    var modelTo = serverRelation.modelTo;

    var fkName = serverRelation.keyTo;
      if (!relationDefn) return

      const type = relationDefn.type
      const modelTo = relationDefn.modelTo

      // don't handle belongsTo in relationships function
      if (!modelTo || type === 'belongsTo') return

      const data = extractIdsFromResource(relationship.data)
      const from = { model, id }
      const to = { model: modelTo, data }
      return linkRelatedModels(relationName, from, to)
    })
  )
}

<MSG> Move belongsTo relationship handling

Change from doing a saveAttributes to save fk
to model, to setting fk on model and letting
the natural save process commit the change.

The main advantage of doing this is to prevent double
operation hooks firing

<DFF> @@ -15,11 +15,12 @@ module.exports = function (app, options) {
       return next();
     };
 
-    if (ctx.method.name !== 'updateAttributes') return next();
+    if (!ctx.method.name === 'updateAttributes') return next();
 
     id = ctx.req.params.id;
     data = options.data;
     model = utils.getModelFromContext(ctx, app);
+
     relationships(id, data, model);
 
     next();
@@ -56,6 +57,10 @@ function relationships (id, data, model) {
     var serverRelation = model.relations[name];
     if (!serverRelation) return;
     var type = serverRelation.type;
+
+    // don't handle belongsTo in relationships function
+    if (type === 'belongsTo') return;
+
     var modelTo = serverRelation.modelTo;
 
     var fkName = serverRelation.keyTo;

Move belongsTo relationship handling