Shanika Ediriweera Shanika Ediriweera - 2 months ago 13
Node.js Question

How to create a list of objects which are one-way associated with objects of another model in SailsJS?

I have models Playlist.js and User.js. I want to have a list of users in Playlist who can answer for the comments on them. I do not want the attribute to appear in User. (i.e. one-way-association)

Playlist.js

module.exports = {
attributes: {
id: {
type: 'integer',
autoIncrement: true,
primaryKey:
},
name: {
type: 'string',
size: 128,
required: true
},
// many to many relationship with role
roles_with_access: {
collection: 'role',
via: 'playlist',
through: 'rolehasplaylist'
},

// THIS ATTRIBUTE ASSOCIATE WITH ONE USER
// BUT INSTEAD OF THAT I WANT A LIST OF USERS
users_who_can_answer_comments: {
model: 'user'
}
}
};


User.js

module.exports = {
attributes: {
id: {
type: 'integer',
autoIncrement: true,
primaryKey: true,
},
name: {
type: 'string',
size: 128,
required: true
},
email: {
type: 'email',
required: true,
unique: true
},
adminRole: {
model: 'role'
}
}
};


Sails documentation have mentioned one-way association only for one-to-one mapping - http://sailsjs.org/documentation/concepts/models-and-orm/associations/one-way-association
I want to find a way to have a list of users with associations to users?

Answer

Change model to collection:

users_who_can_answer_comments: {
    collection: 'user'
}