vendredi 15 janvier 2016

Problems with multiple relations in one model

I am trying to get the participants count of a few target audiences that are related to a scan, and when i am looking into a specific scan it show's me the participant counts related to the target audience of all scans instead the participant counts related to the target audience of that specific scan.

I have the following models:

  • scans
  • participants
  • targetAudience
  • scanParticipants (Makes the relation between the “scan”, “participant” & “targetAudience”, it also has some extra data [pivot table, is what they call it i though])

scan.js model:

import DS from 'ember-data';

var scanModel = DS.Model.extend({
    targetAudiences:             DS.hasMany('targetAudiences', {async: true}),
    scanParticipants:            DS.hasMany('scanParticipants', {async: true}),
    participants:                Ember.computed('scanParticipants.@each.participant', function () {
        return this.get('scanParticipants').then(function (scanParticipants) {
            return scanParticipants.mapBy('participant');
        })
    }),
    participantsCount:           Ember.computed('scanParticipants.[]', function () {
        return this.get('scanParticipants.length');
    }),
    participantsCountFinished:   Ember.computed('scanParticipants.[]', function () {
        return this.get('scanParticipants').filterBy('finished', true).get('length');
    }),
});

export default scanModel;

target-audience.js model:

import DS from 'ember-data';

var targetAudienceModel = DS.Model.extend({
    scanParticipants:            DS.hasMany('scanParticipants', {async: true}),
    participants:                Ember.computed('scanParticipants.@each.participant', function () {
        return this.get('scanParticipants').then(function (scanParticipants) {
            return scanParticipants.mapBy('participant');
        })
    }),
    participantsCount:           Ember.computed('scanParticipants.[]', function () {
        return this.get('scanParticipants.length');
    }),
    participantsCountFinished:   Ember.computed('scanParticipants.[]', function () {
        return this.get('scanParticipants').filterBy('finished', true).get('length');
    }),
});

export default targetAudienceModel;

scan-participant.js model:

import DS from 'ember-data';

var scanParticipantModel = DS.Model.extend({
    finished:       DS.attr('boolean'),
    scan:           DS.belongsTo('scan',           {async: true}),
    participant:    DS.belongsTo('participant',    {async: true}),
    targetAudience: DS.belongsTo('targetAudience', {async: true})
});

export default scanParticipantModel;

If i am going to a specific scangroup it shows me a list of scans of that scangroup and that works great example:

url scangroup/1:

  • scan 1 (Participants count: 14, participants finished: 8)
  • scan 2 (Participants count: 14, participants finished: 4)

Now when i am going to a specific scan i want to show the stats of each targetAudience that belongs to the scan i choose example:

url scangroup/1/scan/2:

  • targetAudience 1 (Participants count: 4, participants finished: 1)
  • targetAudience 2 (Participants count: 2, participants finished: 1)
  • targetAudience 3 (Participants count: 4, participants finished: 0)
  • targetAudience 4 (Participants count: 4, participants finished: 2)

But the stats that i get when i am looking at that specific scan (2) is:

  • targetAudience 1 (Participants count: 8, participants finished: 3)
  • targetAudience 2 (Participants count: 4, participants finished: 4)
  • targetAudience 3 (Participants count: 8, participants finished: 1)
  • targetAudience 4 (Participants count: 8, participants finished: 4)

How can i make it possible that when i am looking at scan 2 that it only shows the participants count that are related to that targetAudience of scan 2?

I just can't figure it out, im already 3 days trying everything but it seems like its impossible.

I am using:

  • Ember 2.1.0
  • Ember data 2.1.0

Thanks in advance!

Kindly regards,

Pascal




Aucun commentaire:

Enregistrer un commentaire