user2483431
user2483431

Reputation: 813

Many-to-many relationships with ember-data models

I am new to emberjs and I'm currently working on a new project. My application has 3 models namely Member, Project and Task.

  1. A member can have many tasks and a task can have many members (Many-to-many)
  2. A project can have many tasks and a task can belong to only one project (One-to-many)
  3. A project can have many members and a member can have many projects (Many-to-many)

For 1 and 2, everything is fine.

However, for the relationship between project and members, a member can either be a simple member of the project or a project manager.

In my database schema, I represented this relationship by having an additional table called project_members and had an additional field called 'role' which can either be 0 (meaning a simple member) or 1 (meaning a project manager).

What would be the best way to represent this using ember-data models? How should I represent the relationship and how and where should I include the additional 'role' field?

Thanks

Upvotes: 1

Views: 442

Answers (1)

Mikko Paderes
Mikko Paderes

Reputation: 840

Not sure if it would work since I haven't tried having multiple attributes that point to one model but you could add these attribute in your project model like

projectManager: DS.belongsTo('member'),
members: DS.hasMany('member')

Then for the member model

projectManagerTo: DS.hasMany('project')
projects: DS.hasMany('project')

Or if you have anyway of knowing the project manager with just the member model data you can use a computed property.

projectManager: function() {
  // some logic to determine which of the members is the project manager
}.property('members')

Upvotes: 0

Related Questions