epilogue icon indicating copy to clipboard operation
epilogue copied to clipboard

Include associated model that uses 'as' property

Open semaja2 opened this issue 8 years ago • 1 comments

I have a model that contains two associates that link to the same model but as separated by using the 'as' property

classMethods: {
      associate: function(models) {
        // associations can be defined here
        Device.belongsTo(models.Users, {as: 'createdBy'})
        Device.belongsTo(models.Users, {as: 'updatedBy'})
      }

Using the epilogue resource 'include' property I can happily include other associated models, but I am unable to work out how to include these two

var deviceResource = epilogue.resource({
    model: models.Device,
    include: [
      {model: models.Network},
      {model: models.DeviceType},
      {model: models.Vendor},
      {model: models.Software},
      {
        model: models.User,
       as: 'createdBy', // this doesnt help
        //primaryKeyField: 'createdById'
        //attributes: ['firstName', 'lastName', 'username']
      }
    ],

The error I get is

var primaryKey = i.model.primaryKeyField, ^

TypeError: Cannot read property 'primaryKeyField' of undefined at /Users/jamesas/Projects/Repositories/dokia.io/node_modules/epilogue/lib/Resource.js:126:31 at Array.forEach (native) at new Resource (/Users/jamesas/Projects/Repositories/dokia.io/node_modules/epilogue/lib/Resource.js:125:18) at Object.epilogue.resource (/Users/jamesas/Projects/Repositories/dokia.io/node_modules/epilogue/lib/index.js:62:20)

Anyone know how I can achieve this? My current work around is to write some milestone middle ware to insert the data i want

semaja2 avatar Jul 21 '16 09:07 semaja2

@semaja2 have you tried using auto association? Otherwise it might be useful to put together a simple test case that we can develop against.

mbroadst avatar Jul 21 '16 16:07 mbroadst