Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Question: Having multiple associations pointing to the same inverse reference #40

Open
johanneslumpe opened this issue Oct 22, 2013 · 0 comments

Comments

@johanneslumpe
Copy link

Hi,

first of all thanks for supermodel - it's a very nice and useful addition to the pool of backbone modules! Currently I'm trying to set up some associations from a project to participants, admins and owners. All of the associations reference the same collection of users and the same type of user-model.

I do this with the following:

Project.has().many('participants', {
  collection: UserCollection,
  inverse: 'projects'
});

Project.has().many('admins', {
  collection: UserCollection,
  inverse: 'projects'
});

Project.has().one('owner', {
model: User,
inverse: 'projects'
});

This will of course lead to the problem that when a user is added as participant, but then is added as admin too, the project in question will be added to the inverse relationship twice. Also, when a user is demoted from being an admin in a project, the project will be removed from the projects() association, even though he's still a participant.

Right now I'm conquering the issue by using the following:

Project.has().many('admins', {
  collection: UserCollection,
  inverse: 'projectsAdmined'
});

Project.has().one('owner', {
  model: User,
  inverse: 'projectsOwned'
});

This way all the projects a user participates in are references through projects(), all the admined projects through projectsAdmined() and all the owned projects through projectsOwned(). But it seems rather clumsy to do it like this. Am I missing a feature of supermodel, which might aid with these associations? Or is my specific use case just a weird edge case?

Or maybe it would be better to juse not have the owned and admined projects as associations at all and just have normal arrays of users models, which can then be filtered by looping though the projects() association and checking ownership and admin-rights on the fly for each project? This would of course include some overhead. Any thoughts?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant